How to Clear Calculated PDF Form Fields When Required Inputs Are Empty
Question details
The user needs to prevent a PDF form calculation, such as a percentage or grade, from displaying a result until the required source fields are populated.

- Product
- PDF Form Editor
- Device & OS
- not provided
- Scenario
- Creating a dynamic PDF form where certain fields automatically calculate grades or percentages based on user input.
- Observed behavior
- The calculated fields evaluate to zero or display incorrect data when the source fields are left blank, rather than remaining visually empty.
Ensure that your PDF editing software supports form field properties and custom JavaScript calculations before attempting to modify the calculation logic.
Use Conditional JavaScript to Check for Empty Source Fields
Modify the custom calculation script of the target field to detect if the source inputs are blank before performing any mathematical operations.
By default, PDF forms may treat empty numerical fields as zero during a calculation. To ensure a field remains blank when inputs are missing, you must read the source fields as strings using 'valueAsString' and apply a conditional 'if' statement.
Open your PDF document in your PDF editor and enter the Form Editing or Prepare Form mode to view all interactive fields.
Right-click on the calculated field (for example, the Percentage or Grade field) and select 'Properties' from the context menu.
In the Properties dialog, click on the 'Calculate' tab. Select the option for 'Custom calculation script' and click the 'Edit' button.
Enter the JavaScript code that checks for empty strings. For example: var s1 = this.getField("DataTest").valueAsString; var s2 = this.getField("StudentInvestigation").valueAsString; if (s1 == "" || s2 == "") event.value = ""; else event.value = ((Number(s1) + Number(s2)) / 30) * 100;
Click 'OK' to save the script, close the Properties dialog, and exit Form Editing mode. Test your form by leaving the source fields blank to confirm the calculated field remains empty.

Design Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive PDF toolkit that allows you to easily design interactive forms, edit field properties, and implement custom calculation logic.
- 1. Open PDF Form Tools: Launch WPS Office, open your PDF document, and navigate to the 'Form' tab on the top ribbon.
- 2. Manage Form Fields: Use the interactive controls to add new Text Fields or select existing ones to prepare documents for users.
- 3. Customize Properties: Double-click any form field to open the Properties menu, where you can modify layout, formatting, and calculation parameters.

Frequently Asked Questions
Why does a PDF form show a zero when calculated fields are empty?
By default, PDF calculation engines treat empty numerical fields as zero. If your calculation involves multiplication or division, it outputs a zero. To prevent this, you need custom JavaScript to explicitly display a blank string when the inputs have no value.
Does marking a field as 'Required' stop it from calculating?
No. Marking a field as 'Required' in its Properties dialog only prevents the user from submitting the form if the field is empty. It does not stop real-time calculations from running in the background.
What is the purpose of 'valueAsString' in the JavaScript calculation?
The 'valueAsString' property fetches the exact text input of a field as a string rather than interpreting it as a number. This is necessary because evaluating an empty field as a number may return zero, whereas reading it as a string accurately identifies it as completely blank ("").




