Skip to main content
kjellcam71
Participant
June 14, 2018
Answered

javascript for sequential fields within a pdf

  • June 14, 2018
  • 3 replies
  • 1420 views

I am trying to find a javascript that will allow all of the fields in my form to populate in sequential order. I have 10 fields on the PDF and I would like them to be numbered sequentially based off of the first field. Any help would be greatly appreciated.

This topic has been closed for replies.
Correct answer BarlaeDC

Hi,

Assuming the fields are numerically ordered ( Field1, Field2, Field3), and that Field1 is the starting field you could use script something like

var startFieldValue = this.getField("Field1").value;

for ( var i = 2; i < 10; i++)

{

     startFieldValue++;

    this.getField ("Field" + i).value = startFieldValue;

}

This will add 1 to the value of Field1, and then push that value to the next field in the sequence.

The code could probably be added to the validate function of "Field1".

Note: there is no checking that the user actually entered a number in the field, which may be required if you haven't applied any validation to the field.

Hope this helps

Malcolm

3 replies

kjellcam71
Participant
June 16, 2018

Thank you. This worked great!

Inspiring
June 15, 2018

You need to write a custom JavaScript action to perform this task and this would require knowing your actual field names. There are a number of ways to accomplish this.

I would use the "On Blur" action so only after one exits the field the next 9 named fields are updated.. The script could look something like:

// array of field names to populate;

var aFieldNames = new Array("Field.1", "Field.2", "Field.3", "Field.4", "Field.5", "Field.6", "Field.7", "Field.8", "Field.9");

for(var i = 0; i < aFieldNames.length; i++) {

     // to to i field in array set value equal to this field value plus array index + 1;

     this.getField(aFieldNames).value = Number(event.value) + i + 1;

} // field name loop;

The above code does not require and systemic field naming convention, but could easily be modified to support such a naming system. Also the fields are only updated when the first field's value is changed. By using the array of the field names, the script is easily scalible and only requiring the names of the fields to be changed and the script automatically adjust the number of fields to process based on the number of field names entered.

Inspiring
June 15, 2018

I am curious why use the On blur event rather than just the validate event of the first field?

Inspiring
June 15, 2018

That field is executed after the validation event and before the next field in the array of field to be processed is processed. It also allows for the UI validation to be used without having to write special code.

BarlaeDC
Community Expert
BarlaeDCCommunity ExpertCorrect answer
Community Expert
June 15, 2018

Hi,

Assuming the fields are numerically ordered ( Field1, Field2, Field3), and that Field1 is the starting field you could use script something like

var startFieldValue = this.getField("Field1").value;

for ( var i = 2; i < 10; i++)

{

     startFieldValue++;

    this.getField ("Field" + i).value = startFieldValue;

}

This will add 1 to the value of Field1, and then push that value to the next field in the sequence.

The code could probably be added to the validate function of "Field1".

Note: there is no checking that the user actually entered a number in the field, which may be required if you haven't applied any validation to the field.

Hope this helps

Malcolm