Fix PDF Form JavaScript Calculation Function Not Updating in Acrobat
Question details
A custom document-level JavaScript function used to multiply two form fields fails to update the destination field when triggered from a validation script.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Attempting to calculate the product of two form fields using a JavaScript function called from a field's custom validation script.
- Observed behavior
- The destination field does not display the calculated result because the script uses improper variable assignment, relies on string values instead of numbers, and incorrectly utilizes validation scripts for field computation.
Ensure you are using the 'Prepare Form' tool in your PDF editor and that you have a basic understanding of JavaScript syntax for PDF forms.
Correct the Script Logic and Field Assignment
Update the JavaScript function to explicitly convert field values to numbers, correctly assign the output to the form field object, and move the execution trigger to a calculation script.
In PDF JavaScript, simply assigning a value to a variable name (like Total01 = result) only creates a local variable in the script; it does not update the actual form field. Furthermore, validation scripts are designed for checking user input, not for performing mathematical computations.
Remove your function call from the field's 'Custom Validation' tab. Instead, open the destination field's properties, go to the 'Calculate' tab, and place your script in the 'Custom calculation script' section. This ensures the calculation runs when field values change.
Avoid using 'valueAsString' when you intend to perform mathematical operations like multiplication. Fetch the field values and explicitly parse them as numbers (e.g., using Number() or multiplying by 1) to prevent unexpected string concatenations.
Do not simply write 'Total01 = result;'. To update the actual form field, use the event object by typing 'event.value = result;' inside the calculation script, or explicitly target the field using 'this.getField("Total01").value = result;'.
If you are keeping the logic in a document-level function, remember that the scope of 'this' changes depending on where the function is called. Pass the document object as a parameter to your function so it can reliably locate 'this.getField'.

Create and Calculate PDF Form Fields with WPS Office
WPS PDF provides an intuitive way to create fillable forms, set up field properties, and apply automated calculations without needing to debug complex document-level JavaScript.
- 1. Open your PDF in WPS: Launch WPS Office, open your PDF document, and navigate to the 'Form' tab.
- 2. Select the destination field: Right-click the text field where you want the calculated result to appear and choose 'Properties'.
- 3. Set up the calculation: Go to the 'Calculate' tab in the Properties window. Choose 'Value is the' and select 'product (x)' from the dropdown menu, then pick the fields you wish to multiply.
- 4. Save your form: Close the properties dialog and save your PDF. The field will now automatically calculate the product whenever the target fields are updated.

Frequently Asked Questions
Why isn't my PDF form field updating after a JavaScript calculation?
This usually happens because the script assigns the calculated result to a standard JavaScript variable instead of the field's value property. Ensure you are using `event.value = result;` or `this.getField("FieldName").value = result;` to output the data to the form field.
What is the difference between validation scripts and calculation scripts in PDF forms?
Validation scripts run when a user attempts to input data into a field to verify the data meets specific rules. Calculation scripts run automatically to compute and display results in a read-only destination field based on the values of other fields.
Should I use valueAsString for math calculations in PDF JavaScript?
No. When performing mathematical operations like multiplication or addition, you need numerical values. Using `valueAsString` fetches the data as text, which can cause mathematical functions to fail or produce incorrect results.




