How to Validate a Custom PDF Field Format (e.g., A19-001)
Question details
The user needs to enforce a specific text pattern (such as starting with A or B, followed by a two-digit year, a hyphen, and three digits) for a custom PDF form field.

- Product
- PDF Editors
- Device & OS
- not provided
- Scenario
- Creating a fillable PDF form where user input must strictly match a custom predefined format like A19-001 or B19-001.
- Observed behavior
- The form field requires a validation script to reject invalid inputs and only accept strings that match the specified regular expression pattern.
Ensure you are using a PDF editor that supports JavaScript for form field validation, and familiarize yourself with basic regular expression (RegEx) syntax.
Apply a Custom Validation Script using JavaScript
Use a JavaScript regular expression in the form field properties to test the input value and reject it if it does not match the required pattern.
This method uses standard PDF JavaScript to validate the format of the text entered by a user. It triggers an alert and clears the input if the format is incorrect.
Open your PDF form in edit mode, right-click the target text field, and select 'Properties' from the context menu.
In the Text Field Properties dialog box, click on the 'Validate' tab at the top.
Select the 'Run custom validation script' option, and then click the 'Edit' button.
Paste the following script into the editor: if (event.value) { if (!/^[AB]\d{2}-\d{3}$/.test(event.value)) { app.alert("Error: Invalid value."); event.rc = false; } }
Click 'OK' to save your script and close the properties window. Switch to interactive mode and test the field by entering both valid (e.g., A19-001) and invalid formats.

Create and Manage Fillable PDF Forms with WPS PDF
WPS PDF provides comprehensive tools for creating, editing, and managing interactive PDF forms. You can easily add text fields, customize properties, and build professional fillable documents.
- 1. Open your PDF: Launch WPS Office and open your PDF document.
- 2. Access form tools: Navigate to the 'Form' tab in the top ribbon menu.
- 3. Insert form controls: Click 'Text Field' or other form controls to insert them into your document workspace.
- 4. Customize properties: Double-click the inserted form field to open the Properties window and define its validation rules or appearance.

Frequently Asked Questions
How do I modify the script to only accept the letter 'C' at the beginning?
You can change the regular expression ^[AB] to ^C in the script. The modified line would be: if (!/^C\d{2}-\d{3}$/.test(event.value))
What does 'event.rc = false;' do in the validation script?
The command event.rc = false rejects the user's input. When it executes, the form field refuses to accept the entered value, prompting the user to correct their input.
Can I apply this validation script to multiple fields at once?
No, custom validation scripts must typically be applied to each form field individually through their respective Properties dialogs. However, you can copy the field after configuring the script to duplicate it.
Why does the script check 'if (event.value)' first?
This condition ensures the validation only runs if the field is not empty. It allows users to clear the field or intentionally leave it blank without triggering an error message.




