How to Return a Blank Field for Zero Input in PDF JavaScript
Question details
The user needs a PDF calculation to return a blank field instead of 'NaN' when input fields evaluate to zero, and also wants a script to count only non-empty form fields.

- Product
- Adobe Acrobat / PDF
- Device & OS
- not provided
- Scenario
- Creating automated calculations within PDF form fields using custom JavaScript.
- Observed behavior
- The PDF form calculates an invalid numeric result ('NaN') when input fields (such as a denominator) are zero, instead of remaining completely blank.
Ensure you are using a PDF editor that supports advanced form editing and custom JavaScript calculations, and identify the exact names of the input fields you intend to reference.
Use Conditional JavaScript to Handle Zero Inputs
Prevent 'NaN' errors by adding an IF statement to check if the denominator is zero before executing the calculation.
When performing division or percentage calculations in a PDF form, a denominator of zero will result in an invalid 'NaN' (Not a Number) output. By converting field values to numbers and verifying them first, you can force the result field to display as blank.
Open your PDF form in editing mode, right-click the field where the final calculation should be displayed, and select 'Properties'.
Go to the 'Calculate' tab, select the 'Custom calculation script' radio button, and click the 'Edit' button to open the JavaScript editor.
Extract the input values as numbers. For example: var v1 = Number(this.getField("Text3").valueAsString); var v2 = Number(this.getField("Text1").valueAsString);
Write an IF statement to test the denominator. If it equals zero, set the event value to a blank string. Otherwise, perform the calculation. Example: if (v2 == 0) event.value = ""; else event.value = 100 - ((v1 / v2) * 100);

Script to Count Only Non-Blank Form Fields
Use a loop to iterate through sequentially named form fields, initializing your counter strictly as a numeric value to avoid concatenating strings.
Easily Edit and Manage PDF Forms with WPS Office
WPS Office offers a comprehensive PDF editor that allows you to open, fill, and manage interactive PDF forms effortlessly. Whether you need to input data, add digital signatures, or edit document text, WPS Office streamlines your workflow.
- 1. Download and install WPS Office: Get the free application from the official website and install it on your device.
- 2. Open your PDF file: Launch WPS PDF and open the document containing your interactive form fields.
- 3. Fill or Edit the form: Navigate to the 'Form' tab to edit existing fields, input your data into text boxes, or manage document calculations.
- 4. Save your document securely: Click 'Save' or 'Export to PDF' to preserve your filled form and custom configurations.

Frequently Asked Questions
Why does my PDF calculation return 'NaN'?
'NaN' stands for 'Not a Number.' This error typically occurs when a JavaScript calculation attempts a mathematical operation on non-numeric values, such as dividing by zero or processing empty text fields that haven't been properly converted to numbers.
How do I access custom calculation scripts in a PDF?
Open your PDF in form editing mode, right-click the target form field, and select 'Properties.' Go to the 'Calculate' tab, choose the 'Custom calculation script' option, and click the 'Edit' button to write or paste your JavaScript code.
What is the difference between .value and .valueAsString in PDF JavaScript?
Using '.value' retrieves the raw object value, which can sometimes be null or behave unpredictably in math operations if the field is empty. Using '.valueAsString' forces the extraction of the data as a string, making it easier to evaluate for blanks ("") before safely converting it with the Number() function.




