You are viewing limited content. For full access, please sign in.

Question

Question

Javascript to generate a sequence number

asked on August 8, 2019 Show version history

I'm creating a sequence number on a form that should be in the following format:

yy-[zero-padded sequence] (i.e. - 19-0001)

I'm using this method to create an automatically increasing sequence number, but that's only the first step.  I need a way to get that number zero-padded to 4 digits, and I need the current year in 2-digit format.

My plan is to put each of these values in a hidden field, then concatenate the values to form the sequence number.

I'm running into the same problem I usually do, which is that I don't know javascript, so if you are able to help me, please keep in mind that I need precise instructions on how to put the javascript in place.  The customer already has the following Javascript in place on this form, so I need to know how to mix any new code in with it:

$(document).ready(function() {
    $('#q16').after($('.action-btn'));
});

 

Thanks in advance!

0 0

Answer

SELECTED ANSWER
replied on August 8, 2019

So, assuming that you are storing the sequence number in a hidden field from a lookup on the form, you can use the following function:

function getSequence() {
  // Get Year portion
  var now = new Date(); //current date
  var fullYear = now.getFullYear().toString(); //current 4-digit year
  var year = fullYear.substring(2, 4); //
  
  // Get sequence # from hidden field (Assuming #q13 is the ID of the hidden field)
  var seq = $("#q13).val();     // CHANGE THIS TO THE ID OF YOUR HIDDEN SEQ FIELD
  var paddedSeq = '0000'+seq;
  var paddedSeq = paddedSeq.substring(paddedSeq.length-4, paddedSeq.length);
  var output = year + '-' + paddedSeq;
  return output;
}

 

Then you can modify the $(document).ready() function as follows...

$(document).ready(function() {
    $('#q16').after($('.action-btn'));

    $('#q23').val(getSequence());    // MODIFY #q23 TO THE ID OF THE FIELD WHERE YOU WANT THE SEQUENCE TO BE STORED
});

0 0
replied on August 8, 2019

Looks like that's exactly what I'm looking for, but after putting it in, it doesn't seem to be doing anything.  Here's my final code:

function getSequence() {
  // Get Year portion
  var now = new Date(); //current date
  var fullYear = now.getFullYear().toString(); //current 4-digit year
  var year = fullYear.substring(2, 4); //
  
  // Get sequence # from hidden field
  var seq = $("#q35).val();
  var paddedSeq = '0000'+seq;
  var paddedSeq = paddedSeq.substring(paddedSeq.length-4, paddedSeq.length);
  var output = year + '-' + paddedSeq;
  return output;
}

$(document).ready(function() {
    $('#q16').after($('.action-btn'));

    $('#q24').val(getSequence());
});

 

As I read it, it's only using the value in the hidden Sequence Number field, and I made sure there's a value there of 2.  The final field that the sequence will be stored in (q24) also has a CSS class assigned to it.  Would that affect how this functions?

0 0

Replies

replied on August 8, 2019

The CSS shouldn't affect it, if you're just changing the format, but I guess it depends on what the CSS is doing. If you're in a test environment or you don't mind seeing a popup on the production form, temporarily, you can use an alert(); function to check the values of your fields to see what is going on.

So, if you want to see what the value of the hidden field is when the function is executing you can put an alert right after the following line:

var seq = $("#q35).val();
alert(seq);   // Displays a popup with the seq value

In Chrome, you can also press F12 in preview mode to bring up the developer console which will give you more information about any javascript errors that might be happening. 

0 0
replied on August 8, 2019

After looking closely at your code, the following line is missing the end quote after the #q35 ...

var seq = $("#q35").val();

0 0
replied on August 8, 2019

So I added the alert because it's still not putting a value in the final field, even after fixing the quotes, and it says the value it's getting from q35 is 0.  I have the field showing, and the actual value is 2.  Is there a way to put a delay in so it runs a second or two after page load?

 

I thought it might because the field type was a single line (text), but I changed it to a number field, and the pop up says "undefined".

0 0
replied on August 8, 2019

Never mind on the "undefined".  That was because the element number changed to q36 when I changed the field.  Once I fixed that in the code, it's back to saying "0", and nothing fills in the final field.

0 0
replied on August 8, 2019 Show version history

Figured out that I needed to format the pointer a bit different to get the value of q36 and to put the final sequence number in the final field.  It needed to be ("#q36 input") and ("#q24 input), respectively.  I also had to add a delay so the hidden sequence field has time to fill before the code runs.  Here's the final code:

var delayInMilliseconds = 2000; //2 seconds

setTimeout(function() {

function getSequence() {
  // Get Year portion
  var now = new Date(); //current date
  var fullYear = now.getFullYear().toString(); //current 4-digit year
  var year = fullYear.substring(2, 4);
  
  // Get sequence # from hidden field
  var seq = $("#q36 input").val();
  var paddedSeq = '0000'+seq;
  var paddedSeq = paddedSeq.substring(paddedSeq.length-4, paddedSeq.length);
  var output = year + '-' + paddedSeq;
  return output;
}

$(document).ready(function() {
    $('#q16').after($('.action-btn'));

    $('#q24 input').val(getSequence());
});

}, delayInMilliseconds);

 

Thanks SO much for your help!

0 0
replied on August 9, 2019

Great. Glad you got it figured out.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.