PDF Editing Issues

How to Distinguish Null from Zero in PDF Form JavaScript

Natalie TaylorNatalie Taylor Oct 28, 2026 869 views

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.

How to Distinguish Null from Zero in Acrobat PDF Form JavaScript
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.
Before you start

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.

Solution 1Recommended

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.

1
Access Form Properties

Open your PDF form in editing mode, right-click the calculated result field, and select 'Properties'.

2
Edit Calculation Script

Navigate to the 'Calculate' tab, choose the 'Custom calculation script' option, and click the 'Edit' button to open the JavaScript editor.

3
Retrieve the Source Field Value

Define a variable to retrieve the value of your source field. For example: `var v = this.getField("Field2").value;`.

4
Add Conditional Check

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 = ""; }`.

5
Execute Calculation on Valid Input

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; }`.

Check for Empty Strings or Null Values Before Calculating
Strict Equality Requirement: Always use the strict equality operator (`===`) instead of the loose equality operator (`==`). This ensures that an actual zero (`0`) entered by the user is not mistakenly treated as a blank field.
Powerful PDF Tool

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. 1. Download and Install: Download WPS Office from the official website and install it on your device.
  2. 2. Open Your PDF Form: Launch WPS Office and open your interactive PDF form using the built-in WPS PDF tool.
  3. 3. Fill Out the Form: Click on the interactive fields to input your data. WPS PDF will seamlessly handle embedded calculations.
  4. 4. Save and Export: Once completed, click 'Save' or export the document to securely share your filled PDF form.
Easily view and interact with calculated PDF form fields.Fully compatible with standard interactive PDF forms and Adobe PDF formats.Lightweight, fast, and feature-rich for everyday document tasks.Cross-platform support across Windows, Mac, Linux, iOS, and Android.
microsoft office alternative - wps office

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.