How to Validate a PDF Form Field Against Values in Other Fields
Question details
The user needs to validate a PDF form field by comparing its input against values calculated from other fields using a JavaScript validation script.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Setting up custom form field validation rules using JavaScript in an interactive PDF document.
- Observed behavior
- To ensure accurate validation, the script must properly quote field names, parse values as numbers, and reject input that falls outside calculated boundaries.
Ensure you have access to PDF form editing tools and basic knowledge of JavaScript. Double-check the exact names of the form fields you intend to reference, as field names are case-sensitive.
Implement JavaScript Validation with Correct Type Conversion
Use JavaScript to fetch other field values, convert them to numbers, and compare them against the current input to validate the entry.
When writing custom validation scripts in PDFs, text values must be explicitly converted to numbers to ensure mathematical comparisons work correctly. Omitting quotation marks around field names or failing to convert strings to numbers are the most common causes of script failure.
Use quotation marks around every field name when fetching them. For example, use this.getField('FieldName').value to read the contents of the target field.
Convert the retrieved field values to numbers using the Number() function before using them in any calculations to avoid string concatenation issues.
Perform your mathematical calculation to establish the upperbound and lowerbound limits based on the numeric values.
Write an if-statement to check whether event.value is greater than the upperbound or less than the lowerbound.
If the condition is met and the value is outside the permitted range, set event.rc = false within your script. This tells the PDF viewer to reject the user's input.

Create and Edit PDF Forms with WPS Office
WPS Office provides a comprehensive PDF Editor that allows you to effortlessly create, edit, fill, and manage interactive PDF forms without needing complex coding.
- 1. Open WPS PDF: Launch WPS Office and open your PDF document using the built-in PDF Editor.
- 2. Access Form Tools: Navigate to the Form tab on the top ribbon to view all available form creation and editing tools.
- 3. Edit or Fill Forms: Click on existing fields to modify their properties, or add new text fields to design your interactive document.
- 4. Save and Export: Once your form is set up, save the document to ensure all interactive fields and properties are securely preserved.

Frequently Asked Questions
Why is my PDF form validation script ignoring the upper and lower limits?
This typically happens if the field values are treated as text strings instead of numbers. Ensure you use JavaScript functions like Number() to convert the field values before performing greater-than or less-than mathematical comparisons.
How do I reject an invalid entry in a PDF form using JavaScript?
You can reject an invalid entry by evaluating the input against your conditions and setting event.rc = false; in your custom validation script. This natively instructs the PDF viewer to discard the user's input.
Does WPS PDF support JavaScript validation in forms?
WPS PDF supports viewing, filling, and creating interactive forms. While basic field formatting and calculations are broadly supported, the execution of highly advanced custom Adobe Acrobat JavaScript validations may vary depending on the specific functions used in the document.




