Copy link to clipboard
Copied
Hello,
I am working on a form using Acrobat XI Pro. I need to populate one of my fields (called Order) with something similar to an invoice number, where I have several letters followed by a number that would need to increment by 1 when clicking on a button:
VS001
VS002
VS003...
I already have a button that increments said number; what I would like to know is how to precede it with (unchanging) letters.
The code for that button is:
this.getField("Order").value = Number(this.getField("Orden").value) + 1;
Right now I am using two adjacent fields where one holds the letters and the other has the sequencing numbers, but I encountered the following problem:
- Numbers don't show any leading zeros (it shows as VS4 instead of VS004)
I'd like a more elegant solution where only one field would be needed, if possible.
How could this be achieved?
Thank you kindly.
You can use something like this:
var text = this.getField("Order").value;
var str = "000" + (Number(text.substr(-3))+1);
this.getField("Order").value = text.substr(0, 2) + str.substr(-3);
Copy link to clipboard
Copied
You can use something like this:
var text = this.getField("Order").value;
var str = "000" + (Number(text.substr(-3))+1);
this.getField("Order").value = text.substr(0, 2) + str.substr(-3);
Copy link to clipboard
Copied
Thank you. Could you please ellaborate? It's not working for me and, since I don't understand much about javascript, I don't quite get the meaning of the lines, so I don't know where I'm wrong.
I'm guessing you're setting properties for both text (limiting it to 2 characters, although I don't understand what the 0 in (0,2) means) and numbers (formatting it to 3 digits and adding 1 each time), and then making the field show as text+numbers.
Should I format the field in any way?
Copy link to clipboard
Copied
Okay, I got it working. I deleted the text that was showing and added VS000 to the default value in the field, now the button works correctly. Still, I would want to know what those lines mean.
Thanks again.
Copy link to clipboard
Copied
You can learn how this (and other) method works here: JavaScript String substr() Method
Copy link to clipboard
Copied
Thanks, I understand now .
Find more inspiration, events, and resources on the new Adobe Community
Explore Now