Fix Acrobat JavaScript If Statement for Zero Values in PDF Forms
Question details
A custom calculation script fails to properly exclude a specific field from an average calculation when its rating value is zero.

- Product
- Device & OS
- not provided
- Scenario
- Writing a custom JavaScript calculation to average three ratings, with a conditional requirement to skip the second rating entirely if it is exactly zero.
- Observed behavior
- The conditional calculation does not work as expected, either throwing an error or including the zero value in the average due to syntax or variable typing errors.
Ensure you have access to the PDF form editing tools and the correct permissions to modify field properties and custom calculation scripts.
Correct Variable Types and the Equality Operator
Fix the script by explicitly converting the form field string values to numbers and using the standard equality operator (==) instead of assignment (=).
In PDF JavaScript, the .value property of a text field often returns a string. If you try to calculate these without conversion, the script may fail. Furthermore, using a single equals sign (=) assigns a value rather than checking a condition, which breaks the 'if' statement logic.
Open your PDF in form editing mode, right-click the target field where the average should display, and select 'Properties'.
Navigate to the 'Calculate' tab, choose 'Custom calculation script', and click the 'Edit' button to view your code.
Update your variable declarations to use the Number() function. For example, change 'var B = this.getField("InitiativeRating").value;' to 'var B = Number(this.getField("InitiativeRating").value);'.
Locate your conditional statement and replace the assignment operator with the equality operator. Change 'if (B = 0)' to 'if (B == 0)'.
Click 'OK' to save the script, exit form editing mode, and test the calculation by inputting a zero into the InitiativeRating field to confirm the average calculates correctly.

Manage Interactive PDF Forms Seamlessly with WPS Office
WPS Office provides a comprehensive, lightweight PDF editor that allows you to easily view, fill, and manage interactive PDF forms, ensuring high compatibility with standard PDF features and calculations.
- 1. Download and Install: Download WPS Office for free from the official website and complete the quick installation.
- 2. Open Your Form: Launch WPS Office, select 'PDF', and open your interactive PDF document containing the calculation fields.
- 3. Fill and Verify Data: Click directly on the interactive form fields to input your values. The embedded calculation scripts will process automatically.
- 4. Save the Document: Once the form calculates correctly, click the 'Save' icon to keep a record of your processed data.

Frequently Asked Questions
Why does my Acrobat JavaScript calculation ignore zero values?
If you don't explicitly parse the input, JavaScript might treat an empty string or a '0' character differently than the mathematical number 0. Wrapping your field value retrieval in the Number() function forces it to be evaluated as a mathematical integer.
What is the difference between = and == in PDF calculation scripts?
A single equals sign (=) is an assignment operator used to assign a value to a variable. A double equals sign (==) is an equality operator used to compare two values inside a conditional 'if' statement.
How can I debug custom calculation scripts in a PDF?
You can use the built-in JavaScript console in your PDF editor to run test scripts, log variable outputs using console.println(), and check for syntax errors before applying the script to the form field.




