How to Distinguish Null from Zero in PDF Form JavaScript
Question details
The user needs a way to prevent a calculated PDF form field from treating an empty value as zero, which causes calculations to run prematurely.

- Product
- Adobe Acrobat / PDF Forms
- Device & OS
- not provided
- Scenario
- Writing custom JavaScript for calculated fields in an interactive PDF form.
- Observed behavior
- The form automatically runs percentage-change and other math calculations before user input because empty fields are incorrectly evaluated as zero.
Ensure you have access to the 'Prepare Form' or 'Form Editing' mode in your PDF editor, and that you are familiar with writing custom JavaScript calculation scripts.
Check for Empty Strings or Null Values Before Calculating
Update your calculation script to explicitly check if the input field is blank or null before executing mathematical operations.
When a PDF form calculates a field, it often casts empty text fields to the number zero. To stop formulas from executing prematurely on blank fields, you must add a conditional statement that skips the calculation if the value strictly equals an empty string or null.
Open your PDF form in editing mode, right-click the calculated result field, and select 'Properties'.
Navigate to the 'Calculate' tab, choose the 'Custom calculation script' option, and click the 'Edit' button to open the JavaScript editor.
Define a variable to retrieve the value of your source field. For example: `var v = this.getField("Field2").value;`.
Add an `if` statement to check for empty or null values and leave the event value blank if true: `if (v === "" || v === null) { event.value = ""; }`.
Add an `else` block to execute your calculation only when a valid number is present: `else { event.value = ((Number(v) - Number(this.getField("Field1").value)) / Number(this.getField("Field1").value)) * 100; }`.

Edit and Fill PDF Forms with WPS Office
WPS Office offers a comprehensive built-in PDF editor that allows you to easily view, fill, and modify interactive PDF forms. Enjoy a seamless document workflow without needing expensive, heavy software.
- 1. Download and Install: Download WPS Office from the official website and install it on your device.
- 2. Open Your PDF Form: Launch WPS Office and open your interactive PDF form using the built-in WPS PDF tool.
- 3. Fill Out the Form: Click on the interactive fields to input your data. WPS PDF will seamlessly handle embedded calculations.
- 4. Save and Export: Once completed, click 'Save' or export the document to securely share your filled PDF form.

Frequently Asked Questions
Why does Acrobat treat my empty PDF form field as a zero?
In PDF JavaScript, the default numeric conversion of an empty string is zero. Unless explicitly handled with an if-statement checking for an empty string, mathematical operations will interpret the blank input as a `0`.
Can I use standard JavaScript methods like isNaN() to check for empty fields?
While `isNaN()` checks if a value is Not-a-Number, an empty string converted to a number evaluates to `0`, which is a valid number. Therefore, `isNaN()` will not catch an empty string. Strictly checking `v === ""` is required.
How do I hide the calculation result if the required fields aren't filled yet?
You can set `event.value = "";` in your custom calculation script whenever the required input fields are evaluated as empty or null. This forces the result field to remain blank until valid data is entered.




