These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Acrobat Javascript concatenates if only one numerical field has data

suewhitehead
Registered: Jun 3 2008
Posts: 232
Answered

I have an Acrobat javascript that is set up to sum several fields, then multiply the sum by 8% tax and add the sum and the tax together. It works except that if only one field has data entered into it, then the result concatenates. What do I need to add or correct so that with data in only one field, it figures the tax and adds it to the data in the field instead of concatenating the two?

Here is the script:

var sum = 0;
for(var i = 0; i < 2; i++)
{
var cFldName = "field0_" + + i;
sum += this.getField(cFldName).value;
}
event.value = sum + (sum * .08);

My Product Information:
Acrobat Pro 9.0, Windows
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1875
Whenever you use the + operator for numerical addition, you should make sure each value is a (finite) number. For numeric fields that might be blank, you can use the unary + operator to force a conversion of an empty string to a number (0), so your code could be modified to:

var sum = 0;

for (var i = 0; i < 2; i++) {
var cFldName = "field0_" + i;
sum += +getField(cFldName).value;
}

event.value = 1.08 * sum;



George
suewhitehead
Registered: Jun 3 2008
Posts: 232
Thanks. This is exactly what I needed.