How to Validate Four-Digit Codes and Reject Invalid Patterns in PDF Forms
Question details
The user needs to configure a PDF form field to accept four-digit codes while automatically rejecting specific invalid number patterns, such as repeated or sequential digits.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Setting up form validation rules in an interactive PDF where listing every valid code combination is impractical.
- Observed behavior
- The form field requires an automated way to detect restricted sequences and alert the user without manually exhaustively listing all accepted entries.
Ensure you have access to a PDF editor with advanced form editing capabilities that supports custom JavaScript for field validation.
Use JavaScript to Validate PDF Form Fields
Implement a custom JavaScript validation script in the form field properties to define an array of invalid values and check user input against it.
Listing every valid four-digit code is highly inefficient. Instead, you can define an array of explicitly restricted values (such as repeated digits or consecutive numbers). By leveraging the JavaScript indexOf function, the form can instantly check if the user's input matches any of these invalid patterns.
Open your PDF in form editing mode, right-click the designated text field, and select Properties from the context menu.
In the Properties dialog box, click on the Validate tab to set up input restrictions for the selected field.
Select the option to 'Run custom validation script' and click the Edit button to open the JavaScript editor window.
Enter the following code to define invalid sequences and change the text color to red if an invalid code is detected: var invalidValues = ['0000', '1111', '2222', '3333', '0123', '1234', '2345', '3456']; if (event.value != '' && invalidValues.indexOf(event.value) != -1) { app.alert('The entered value cannot be four identical digits or four consecutive digits!'); event.target.textColor = color.red; } else { event.target.textColor = color.black; }
Click OK to save the script, close the Properties window, and enter a test value like '0000' in the field to verify that the validation alert appears.

Create and Manage Interactive PDF Forms with WPS Office
WPS Office provides comprehensive PDF editing features, allowing you to design interactive forms, configure field properties, and manage validation efficiently without advanced programming knowledge.
- 1. Install WPS Office: Download and install the free WPS Office suite on your computer.
- 2. Open Your PDF Form: Launch WPS PDF and open the interactive form document you wish to edit.
- 3. Access Form Controls: Navigate to the 'Form' tab on the top ribbon to view all available form creation and editing tools.
- 4. Modify Field Properties: Double-click any text field to access its properties, where you can define formatting, character limits, and validation rules.

Frequently Asked Questions
How can I restrict the PDF form field to exactly four characters?
To enforce a character limit, open the text field Properties, navigate to the Options tab, check the 'Limit of' box, and enter '4' in the characters field.
Can I prevent users from typing letters in the number code field?
Yes. In the field Properties, go to the Format tab, select 'Number' from the category dropdown, and adjust the decimal places to zero. This restricts the input to numeric digits only.
Why isn't my JavaScript validation working when viewing the PDF?
Some PDF viewers disable JavaScript for security reasons. Ensure that JavaScript execution is enabled in the Preferences or Trust Center settings of the PDF reader being used to view the form.




