Fix an Acrobat Form JavaScript Error When Dividing by a Field
Question details
Correct an Acrobat form calculation that multiplies one field by 200,000 and divides it by another, returning NA when the divisor is blank or zero.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form uses a custom JavaScript calculation to divide the value of LogTot1 by Hours1 after applying a multiplier.
- Observed behavior
- The script reports a syntax error because it attempts to declare a variable inside the if condition.
Open the PDF in Acrobat and confirm that the source fields are named LogTot1 and Hours1. Identify the result field where the custom calculation script is stored.
Correct the custom calculation script in Acrobat
Declare the field variables before the condition and assign NA when the divisor is blank or zero.
In Acrobat, open the PDF and select Prepare a Form. Double-click the field that should display the calculation result.
In the field properties, select Calculate, choose Custom calculation script, and click Edit.
Enter this script, adjusting the field names if necessary: var nA = Number(this.getField("LogTot1").valueAsString); var nB = Number(this.getField("Hours1").valueAsString); if (nB === 0) { event.value = "NA"; } else { event.value = (nA * 200000) / nB; }
Click OK to save the script, close the field properties, and exit form editing. Enter values in LogTot1 and Hours1, then test Hours1 as blank and as zero; both should display NA.

Work with PDF and Office files in WPS Office
WPS Office is a free, lightweight alternative for everyday document work, with a familiar interface and Microsoft Office format compatibility to make moving existing files easier. Use it to view and work with PDFs, although it does not fix Acrobat's form JavaScript.

Frequently Asked Questions
Why does declaring a variable inside the if condition cause an error?
An if condition must contain an expression, not a var declaration. Declare nB before the if statement, then test its value inside the parentheses.
Why does the script check only whether nB is zero?
Number() converts both a blank field value and a numeric zero to 0. The condition nB === 0 therefore covers both cases.
Where do I find the custom calculation script?
In Acrobat, select Prepare a Form, open the result field's properties, and go to Calculate. Select Custom calculation script and click Edit.
What if NA does not display in the result field?
Open the result field's properties and check the Format tab. A numeric-only format may prevent the text NA from displaying correctly.




