How to Write a JavaScript Validation Formula for a Numeric Range in PDF Forms
Question details
The user needs to write a JavaScript validation script for a PDF form field to check if an entered number falls within a specific numeric range, specifically between -10 and 10.

- Product
- Device & OS
- not provided
- Scenario
- Setting up an interactive PDF form field that validates numeric input and modifies the field value based on custom criteria.
- Observed behavior
- The user wants the PDF field value to evaluate whether the entered number is within an inclusive range and set a corresponding value if the condition is met.
Ensure you have access to a PDF editing tool that supports custom form field properties and JavaScript execution, such as Adobe Acrobat Pro.
Use the Logical AND Operator for Range Validation
Combine two conditional checks using the logical AND (&&) operator to ensure the entered value falls within both the maximum and minimum boundaries.
To validate a numeric range in a PDF form field, you need to verify that the value is simultaneously less than or equal to the upper limit and greater than or equal to the lower limit. The logical AND (&&) operator successfully links these two conditions.
Open your PDF form in edit mode, right-click the target text field (e.g., 'B22'), and select 'Properties' from the context menu.
Navigate to the 'Validate' tab in the Properties dialog box, select the 'Run custom validation script' option, and click the 'Edit' button.
Input the following script: if (event.value <= 10 && event.value >= -10) { event.value = 0; }. This checks if the input is between -10 and 10 inclusive.
Click 'OK' to save the script, close the Properties window, and test your form by entering numbers inside and outside the defined range.
Fill and Manage Interactive PDF Forms Seamlessly with WPS PDF
While creating custom JavaScript validation requires advanced editor capabilities, filling out, managing, and interacting with those validated PDF forms is effortless in WPS Office. WPS PDF provides a smooth, reliable experience for all your standard form-filling needs.
- 1. Open Your PDF Form: Launch WPS Office and open your interactive PDF document using the built-in WPS PDF tool.
- 2. Access Interactive Fields: Click on any text field, checkbox, or dropdown menu. The form will recognize standard embedded validations.
- 3. Fill and Save: Enter your numeric or text data into the fields, verify the inputs, and click 'Save' to preserve your filled document.

Frequently Asked Questions
Why isn't my PDF JavaScript validation triggering when I type?
Ensure that the form field's validation trigger is configured correctly in the Properties panel under the Validate tab. Also, verify there are no syntax errors, such as missing brackets or typos, in your custom JavaScript code.
Can I use multiple logical operators in one validation script?
Yes, you can combine multiple conditions using logical AND (&&) as well as logical OR (||) operators to create more complex validation rules for your PDF form fields.
How do I show an error message if the PDF form validation fails?
You can include an 'else' block in your script and use the app.alert('Your error message'); function to display a pop-up warning when a user enters an invalid number outside of your specified range.
Are custom JavaScript validation scripts compatible across all PDF readers?
Basic interactive form filling is widely supported, but custom JavaScript execution may require robust desktop software like Adobe Acrobat or advanced dedicated PDF editors to function properly on the user's end.




