How to Validate Multiple PDF Form Formats with Acrobat JavaScript
Question details
The user needs to restrict an Acrobat PDF text field to allow only one of three specific formats using a custom keystroke script and regular expressions.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Setting up advanced form field validation to restrict user input to specific patterns like custom IDs, dates, or serial numbers.
- Observed behavior
- The user wants to implement a script that rejects non-matching values and displays a validation error message.
Ensure you have access to the PDF form editing tools and a basic understanding of JavaScript Regular Expressions (RegExp) before applying custom validation scripts.
Use Document-Level Functions and Custom Keystroke Scripts
Add a centralized JavaScript validation function and call it from the text field to verify input against required regular expressions.
Using the JavaScript RegExp object allows you to strictly format and validate keystrokes. Defining this function at the document level makes it easier to manage and reuse across multiple form fields.
Go to Tools > JavaScript > Document JavaScripts. Create a new script, name it, and define a function that tests the input string against your three allowed regular expressions.
Right-click the target text field, select 'Properties', and navigate to the 'Format' or 'Validate' tab.
Select 'Custom Keystroke Script' (or 'Custom Validation Script') and input the code to call your document-level function.
Within your script, write conditional logic to reject values that return 'false' from your RegExp test. Include an app.alert() function to display a helpful validation message to the user.

Create and Fill PDF Forms with WPS PDF
While Acrobat JavaScript handles complex programmatic scripting, WPS PDF offers a highly intuitive and user-friendly interface for creating, editing, and interacting with fillable PDF forms without the need for advanced coding skills.
- 1. Open your PDF Document: Launch WPS Office and open the PDF file you wish to modify or fill out.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view available interactive elements.
- 3. Add and Configure Fields: Insert text fields, checkboxes, and dropdown menus, then adjust their properties for correct data entry.
- 4. Save and Share: Save your newly created or filled PDF form and securely share it with your team.

Frequently Asked Questions
What is the RegExp object in Acrobat JavaScript?
The RegExp object allows you to define regular expressions, which are sequences of characters that specify a search pattern. It is used in PDF forms to enforce specific text formats like emails, IDs, or phone numbers.
Can I validate multiple different formats in a single PDF text field?
Yes. You can use the JavaScript logical OR operator (|) within a single regular expression, or use an if-else statement in your custom script to check the input against multiple RegExp patterns.
Where can I learn more about specific Acrobat form JavaScript?
While standard JavaScript guides are helpful, books like John Deubert's 'Extending Acrobat Forms with JavaScript' focus specifically on Acrobat's unique API, formatting rules, and keystroke validation techniques.
Why does my custom validation script fail to trigger?
Ensure that the script is placed in the correct tab (Validation or Keystroke) within the Field Properties. Also, check the JavaScript console (Ctrl+J or Cmd+J) for syntax errors that might be preventing the script from executing properly.




