PDF Editing Issues

How to Restore a PDF Form Value When JavaScript Validation Fails

Huma Ashraf ChHuma Ashraf Ch Nov 10, 2026 869 views

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.

How to Restore a PDF Form Value When Validation 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.
Before you start

Ensure you are in PDF editing mode and have access to the specific form field's properties and validation script settings.

Solution 1Recommended

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.

1
Open Field Properties

In your PDF editor's form mode, right-click the field you want to validate and select 'Properties'.

2
Navigate to the Validation Tab

Click on the 'Validate' tab and select the option to 'Run custom validation script'.

3
Enter the Validation Script

Add your conditional logic. Inside the failing condition block, add 'event.rc = false;' followed by 'app.alert("Your error message");'.

4
Save and Test

Close the properties window, exit form editing mode, and test the field by entering an invalid value to ensure it successfully reverts.

Use event.rc to Cancel the Validation Event
Validation Script Placement: Always place this script in the field's specific Validation event, not in the Calculation or Format events, to ensure 'event.rc' functions correctly.
Advanced PDF Form Editing

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. 1. Open the PDF File: Launch WPS Office and open your PDF document.
  2. 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access interactive form creation tools.
  3. 3. Configure Properties: Double-click any form field to open its properties and apply your custom validation logic.
Easily add interactive form controls like text fields, checkboxes, and buttons.Set up advanced properties and field validations seamlessly.Fully compatible with standard PDF form formats and JavaScript rules.
microsoft office alternative - wps office

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.