How to Prevent PDF Form Division by Zero with JavaScript
Question details
The user needs to prevent calculated PDF form fields from displaying an error or an 'Infinity' value when a denominator field is left blank or equals zero.

- Product
- Device & OS
- not provided
- Scenario
- Creating automated math calculations in a PDF form where division is required, but the divisor (denominator) might temporarily be empty or zero.
- Observed behavior
- The form field displays an unwanted division by zero error or 'Infinity' when the denominator is blank or zero, disrupting the form's layout and logic.
Verify the exact names of the form fields involved in your calculation (e.g., 'FieldA', 'FieldB'), as JavaScript field references are case-sensitive.
Use a Custom Calculation Script to Check for Zero
This is the most reliable method. By using a custom JavaScript calculation, you can check if the denominator is zero before attempting the division, safely outputting a blank field instead of an error.
Standard simplified field notations cannot handle conditional logic like division by zero. Using a custom calculation script allows you to intercept the process, convert all field values to strict numbers, and evaluate the denominator before applying the mathematical operator.
Open your PDF form in editing mode. Right-click the field where you want the calculated result to appear and select 'Properties'.
In the Properties dialog box, click on the 'Calculate' tab to manage the field's calculation methods.
Choose the 'Custom calculation script' option, then click the 'Edit' button to open the JavaScript editor.
Enter the following script, replacing the field names with your own: var a = Number(this.getField('FieldA').valueAsString); var b = Number(this.getField('FieldB').valueAsString); var c = Number(this.getField('FieldC').valueAsString); if (c == 0) event.value = ''; else event.value = (a + b) / c;
Click 'OK' to save the script, close the Properties window, and test your form by leaving the denominator field ('FieldC') blank to ensure no error appears.

Use a Custom Format Script to Hide Infinity
If you prefer not to change your calculation method, you can use a formatting script to catch the resulting 'Infinity' value and display a custom text like 'NA'.
Create and Edit Fillable PDF Forms with WPS Office
WPS Office provides a highly compatible PDF editor that allows you to easily manage PDF forms, add interactive fields, and organize pages without complex external tools.
- 1. Open Your PDF Document: Launch WPS Office and open your existing PDF form file.
- 2. Access the Form Tools: Navigate to the 'Form' tab on the top ribbon to access various interactive field tools like text fields, checkboxes, and buttons.
- 3. Modify Form Properties: Double-click any form field to open its properties, where you can define formats, calculations, and behaviors.
- 4. Save Your Form: After making your adjustments, click 'Save' or press Ctrl+S to secure your interactive form layout.

Frequently Asked Questions
Why does my PDF form show 'Infinity'?
When a formula inside your PDF form attempts to divide a number by zero or a completely blank field, the mathematical result is undefined. The PDF reader interprets this division by zero mathematically and outputs the word 'Infinity'.
What is the difference between 'value' and 'valueAsString' in PDF JavaScript?
Using 'valueAsString' retrieves the exact content of the field as text. Wrapping it in the 'Number()' function safely converts blank or string inputs into a numeric format (usually zero). Relying solely on 'value' can sometimes yield unpredictable object types, causing script calculations to fail.
Can I use Simplified Field Notation to prevent division by zero?
No, Simplified Field Notation in PDF forms cannot handle conditional logic (like IF/THEN statements). Because preventing a division by zero requires checking the denominator's value before doing the math, you must use a Custom Calculation Script.




