How to Correct Acrobat JavaScript for Dividing Fields with a Maximum Value
Question details
The user needs a correct JavaScript snippet for a PDF form that divides the values of two fields and caps the resulting calculation at a maximum value of 2.

- Product
- Adobe Acrobat / PDF Forms
- Device & OS
- not provided
- Scenario
- Creating a custom calculation script in a PDF form field to divide values and enforce a maximum limit.
- Observed behavior
- The original script fails to calculate correctly due to syntax errors, incorrect capitalization, or illogical comparisons, requiring a properly structured JavaScript formula.
Ensure you have the exact text field names (e.g., 'Text101', 'Text102') used in your PDF form, as JavaScript references are strictly case-sensitive.
Use Correct JavaScript Syntax with Math.min Function
Implement a properly formatted JavaScript calculation using the ternary operator to prevent division by zero, and Math.min to cap the result.
PDF form JavaScript requires strict adherence to syntax, including lowercase keywords. When dividing two fields, it is crucial to handle potential division by zero if the denominator field is empty. Additionally, instead of using complex if/else statements to cap a value, the Math.min() function provides a clean and efficient solution.
Open your PDF in form editing mode, right-click the field where you want the result to appear, and select Properties.
Click on the 'Calculate' tab and select the 'Custom calculation script' option.
Enter the script to define your variables and retrieve the field values as numbers: `var a = Number(this.getField("Text101").value); var b = Number(this.getField("Text102").value);`
Add the division logic with a check for zero, and cap it using Math.min: `var result = b ? a / b : 0; event.value = Math.min(result, 2);`

Create and Edit Advanced PDF Forms with WPS Office
WPS Office features a robust PDF editor that supports form creation, text field management, and interactive PDF forms, allowing you to streamline your document workflow with ease.
- 1. Open your PDF in WPS Office: Launch WPS Office and open your PDF document using the integrated WPS PDF tool.
- 2. Access Form editing tools: Navigate to the 'Form' tab on the top ribbon to view all available form controls and field settings.
- 3. Edit field properties: Double-click on any text field to open its properties panel, where you can manage formatting, actions, and calculations.

Frequently Asked Questions
Why is my PDF JavaScript calculation not working?
Common reasons include syntax errors like using an uppercase 'If' instead of 'if', incorrect field names, or division by zero. JavaScript in PDF forms is strictly case-sensitive.
How do I prevent a division by zero error in PDF forms?
You can use a conditional (ternary) operator to check if the denominator is zero before dividing. For example: `var result = b ? a / b : 0;` ensures the script returns 0 instead of an error when 'b' is empty or zero.
How can I set a maximum limit on a calculated PDF field?
Use the `Math.min(calculatedValue, maximumValue)` function in your custom calculation script. It automatically compares your calculation against the maximum value and outputs whichever is smaller.




