Help with adding a number by one increment with each instance of "reset button"

I am trying to create script that will always add 1 to the previous number saved in a form. For example, if the form submission number is "maJunDept00001", then I'd like the user to be able to click a button that will reset all fields except the name and project description, and then create the new submission number. I have no problem with the reset features but I'm stuck on the best approach to consecutively number the form.

I THOUGHT I could just simply add a +1 behind the string but I can't arrive at where I'm supposed to put the script to make this happen. I thought this would be in the button's action script area. Please advise. Thanks!!


Marla Araiza


8 Answers

Voted Best Answer

I wrote for you this generic code to increment the number part of the value while maintaining the prefix part as is. You will have to take care of the rest elsewhere:

var f = this.getField("MonthlySubmission");
var v = f.value.match(/\d+$/)[0];
f.value = f.value.replace(v, util.printf("%0"+v.length+"d", Number(v)+1));

.


Visit my custom-made PDF scripts website: http://try67.blogspot.com
Contact me personally: try6767@gmail.com


By Gilad D (try67)   

The problem is that the value of your field is not a pure number, which means you'll have to split it in two parts and then change the value of the number while keeping the other text the same.

What's the name of the field you want to edit, by the way?

.


Visit my custom-made PDF scripts website: http://try67.blogspot.com
Contact me personally: try6767@gmail.com


Gilad D (try67)   

Thanks for the quick response. My field name is "MonthlySubmission" and the prefix (and text portion - in the example "maJunDept") is pulling from 3 other fields based on what the user selects in the drop down boxes. In the MonthlySubmission box, I have created the calculation script below. So if I understand correctly, I'd need to create a hidden field? that will hold the number value that I can pull over to the "Monthly Submission" box? Thanks!!

var fullName = this.getField("SavingsOwner").value;
var B = this.getField("MonthofSavings").value;
var C = this.getField("Entity").value;
if (fullName=="")) event.value = "";
else {
var names = fullName.split(" ");
var initials = "";
for (var i in names)
initials+=names[i].charAt(0).toUpperCase();
event.value = initials + B + C;}


Marla Araiza   

No, that's not necessary. But you want the button field to just increment the number part of the value, right? And keep the rest of it as is?

.


Visit my custom-made PDF scripts website: http://try67.blogspot.com
Contact me personally: try6767@gmail.com


Gilad D (try67)   

Well....KIND OF. Yes, I do want the number to always increment by one but it's possible that part of the prefix may change as well (the month or the Entity). When they click the button, what I want to happen is all the fields on the form reset and the form somehow know to count up to the next increment and let the user make the other selections to populate the id. Example, the second, third and fourth forms might be:

maJulydept00002
maAugOtherdept00003
maAugdept00004

Thanks.


Marla Araiza   

But if all the fields in the form reset that means that also the fields you're using to construct the prefix part of the field will reset, and hence also the value itself (since the calculation script will execute), unless you exclude them, of course.


Gilad D (try67)   

Yes, sorry, I thought of that after I replied. What I'm thinking I could do is just not reset the Savings Owner name, Month and Entity - then if you could help me with the auto increment, the user could just select the appropriate month and Entity to update the prefix after starting the new form. Does that make sense? There are only about 20 users that I will be training so I think I can make that point clear. The intent of the "auto-populated" ID is to prevent the user from having to manually count up themselves and, also, allow some sort of consistent identifiers within the ID.


Marla Araiza   

As always, thanks for all the help. One of these days, hopefully the solutions will come to me without having to reach out.


Marla Araiza   


Please specify a reason: