How to Restore a PDF Form Value When JavaScript Validation Fails
Question details
The user needs a way to automatically revert a PDF form field to its previous value if a custom JavaScript validation check (such as comparing two totals) fails.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Setting up custom field validations in an interactive PDF form to ensure correct numeric inputs.
- Observed behavior
- When an invalid value is entered, the form should display an alert message, completely reject the new input, and instantly restore the field's previous valid value.
Ensure you are in PDF editing mode and have access to the specific form field's properties and validation script settings.
Use event.rc to Cancel the Validation Event
By explicitly setting the validation event return code to false, you can force the PDF viewer to reject the invalid input and keep the previous value.
When using custom JavaScript to validate PDF form fields, simply showing an alert box is not enough to stop the field from updating. You must explicitly tell the PDF viewer to reject the change.
The correct method is to use 'event.rc = false;'. When the validation condition fails, this command cancels the update and automatically restores the field's original value.
In your PDF editor's form mode, right-click the field you want to validate and select 'Properties'.
Click on the 'Validate' tab and select the option to 'Run custom validation script'.
Add your conditional logic. Inside the failing condition block, add 'event.rc = false;' followed by 'app.alert("Your error message");'.
Close the properties window, exit form editing mode, and test the field by entering an invalid value to ensure it successfully reverts.

Create and Validate PDF Forms Easily with WPS Office
WPS Office offers comprehensive PDF editing tools, allowing you to quickly create interactive forms, add text fields, and configure custom validation rules without hassle.
- 1. Open the PDF File: Launch WPS Office and open your PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access interactive form creation tools.
- 3. Configure Properties: Double-click any form field to open its properties and apply your custom validation logic.

Frequently Asked Questions
Why does my PDF field accept the value even after an alert box appears?
If you only use app.alert(), the script warns the user but does not actively stop the input. You must include 'event.rc = false;' in your script to instruct the PDF viewer to reject the new value.
Can I use this script to compare two different fields in the PDF?
Yes. You can use JavaScript to retrieve the value of another field (e.g., this.getField('FieldB').value) and compare it against the current field's input (event.value) before deciding to set event.rc to false.
Where exactly should the JavaScript be pasted?
The script should be placed in the 'Validate' tab of the specific form field's Properties menu, under the 'Run custom validation script' option.




