How to Use JavaScript If-Else Statements for Calculated Form Fields in PDF
Question details
The user needs a JavaScript calculation script for a PDF form field to conditionally evaluate a number using if-else logic, while properly handling empty input fields.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating an automated calculation field in a PDF document where the result changes based on a specific numerical threshold.
- Observed behavior
- The calculated field needs to output a specific value if the source number is below a threshold, leave it blank if above, and prevent treating empty fields as zeroes.
Ensure your PDF editor supports custom JavaScript for form calculations, and write down the exact names of your source form fields (e.g., 'G8' or 'p1_L1') before writing the script.
Use Custom JavaScript with Empty Field Handling
The best approach is to first check if the field is empty as a string before evaluating the numerical value to prevent zero-value calculation errors.
In PDF JavaScript, an empty text field is often evaluated as a numerical '0' by default. By retrieving the value as a string first, you can instruct the script to leave the calculated field blank if no data has been entered yet.
Right-click the target form field where you want the result to appear and select 'Properties' from the context menu.
In the Field Properties dialog box, click on the 'Calculate' tab at the top.
Choose the 'Custom calculation script' radio button and click the 'Edit' button.
Paste the following script, adjusting the field name 'G8' as needed: var a = this.getField("G8").valueAsString; if (a == "") { event.value = ""; } else if (Number(a) <= 56) { event.value = 67; } else { event.value = ""; }
Click 'OK' to close the editor, then click 'Close' on the Properties dialog. Test the form by entering numbers in the source field.

Use a Basic If-Else Statement for Numerical Evaluation
Use a simpler if-else condition when you only need to return two fixed values and empty field handling is not a strict requirement.
Manage and Fill Interactive PDF Forms with WPS Office
WPS Office provides a highly capable PDF toolkit that allows you to easily view, fill, and manage interactive PDF forms, offering excellent compatibility with complex PDF documents.
- 1. Launch WPS Office: Open WPS Office and navigate to the PDF module.
- 2. Open Your PDF Form: Click 'Open' and select your interactive PDF document from your local storage.
- 3. Fill the Form Fields: Click directly on the interactive fields to input your data. Calculated fields will automatically update based on the document's built-in scripts.
- 4. Save Your Work: Click the 'Save' icon or press Ctrl+S to securely save your completed PDF form.

Frequently Asked Questions
Why does my PDF JavaScript treat an empty field as zero?
By default, PDF JavaScript often evaluates empty fields as '0' when performing numerical operations via the '.value' property. To prevent this, use the '.valueAsString' property to check if the field is truly empty ("") before executing any mathematical calculations.
How do I find the correct field name for my JavaScript calculation?
You can find the exact field name by entering Form Editing mode in your PDF software. The name of the field is usually displayed directly on the form field placeholder, or you can right-click the field and view its 'Name' in the General tab of the Properties dialog.
Can I chain multiple if-else statements in PDF custom calculations?
Yes, you can use multiple 'else if' blocks within your custom calculation script to handle a variety of numerical ranges and return different specific values for each condition.




