How to Validate PDF Form Values and Change Colors Using JavaScript
Question details
The user needs a JavaScript solution for a PDF form to validate specific text inputs (such as N/A, OK, NOK) and numeric ranges, dynamically changing the text color to green for valid entries and red for invalid ones, while showing a confirmation alert.

- Product
- PDF Editors
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms with advanced custom validation and dynamic formatting rules.
- Observed behavior
- The existing JavaScript logic fails to properly handle text values mixed with numeric ranges, resulting in incorrect validation, failed logic conditions, and improper formatting behaviors.
Ensure your PDF editing software supports JavaScript for form fields, and always back up your original PDF document before applying custom scripts.
Implement Logical JavaScript Validation for PDF Fields
Use a structured JavaScript approach that checks for valid text strings before parsing numbers, ensuring accurate validation and dynamic color changes.
The key to handling mixed inputs (like "N/A" and numbers) is to isolate the text conditions first. Incorrect logic, such as '(v < 552 || v > 552)', simply excludes the number 552 rather than defining a proper range.
By evaluating text inputs before using the Number() function, and using logical AND (&&) for numeric ranges, you avoid syntax errors and achieve reliable validation.
Right-click the target form field in your PDF editor's form editing mode and select 'Properties'.
Go to the 'Validate' tab, select the option to 'Run custom validation script', and click 'Edit' to open the JavaScript editor.
Add an if statement to check if event.value matches your allowed text strings (e.g., 'N/A', 'OK', 'NOK'). If it matches, set event.target.textColor = color.green; to format it as valid.
Add an else block that converts the input into a number using var v = Number(event.value);. Use strict logical conditions like if (v >= 0 && v <= 551) to validate the inclusive range, and set the text color to green if it passes.
If the parsed number falls outside the valid range, use an else block to set event.target.textColor = color.red; and trigger an alert using app.alert('Invalid input'); to notify the user.

Easily Edit and Manage PDF Forms with WPS Office
WPS Office provides comprehensive PDF tools allowing you to create, fill, and manage interactive PDF forms effortlessly. It is highly compatible with standard PDF formats, making document management seamless.
- 1. Open your PDF Document: Download and install WPS Office, then open your PDF form using the WPS PDF tool.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access the dedicated form editing and filling tools.
- 3. Edit and Save: Interact with text fields, checkboxes, and buttons natively, then save or export your fully functional PDF.

Frequently Asked Questions
Why does my PDF JavaScript validation fail on text inputs?
JavaScript validation often fails when text inputs like 'N/A' are accidentally parsed as numbers, returning a 'NaN' (Not a Number) error. To prevent this, your script should explicitly check for and accept specific text strings before attempting to convert the input value into a number using the Number() function.
How do I change the text color of a PDF field using JavaScript?
You can change the text color dynamically by modifying the event.target.textColor property in your custom validation script. For example, use event.target.textColor = color.green; for valid inputs and color.red for errors.
Are JavaScript validations supported in all PDF viewers?
No, not all PDF viewers support JavaScript execution. While fully supported in advanced editors like Adobe Acrobat and WPS Office, built-in browser PDF viewers or basic mobile reader apps might not execute custom validation scripts or format changes.
How do I correctly define a numeric range in PDF JavaScript?
To define an inclusive numeric range, use the logical AND operator (&&). For example, to check if a value is between 0 and 551, use if (v >= 0 && v <= 551). Using the OR operator (||) incorrectly can inadvertently accept numbers outside your intended range.




