PDF Editing Issues

How to Validate Four-Digit Codes and Reject Invalid Patterns in PDF Forms

Huda QurayshiHuda Qurayshi Oct 28, 2026 870 views

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.

How to Validate Four-Digit Codes in PDF Forms
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.
Before you start

Ensure you have access to a PDF editor with advanced form editing capabilities that supports custom JavaScript for field validation.

Solution 1Recommended

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.

1
Access Form Field Properties

Open your PDF in form editing mode, right-click the designated text field, and select Properties from the context menu.

2
Navigate to the Validate Tab

In the Properties dialog box, click on the Validate tab to set up input restrictions for the selected field.

3
Add Custom Validation Script

Select the option to 'Run custom validation script' and click the Edit button to open the JavaScript editor window.

4
Input the JavaScript Code

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; }

5
Save and Test the Form

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.

Use JavaScript to Validate PDF Form Fields
Expanding the Invalid Values List: You can continuously expand the 'invalidValues' array in the script by separating new quoted patterns with commas.
Professional PDF Editor

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. 1. Install WPS Office: Download and install the free WPS Office suite on your computer.
  2. 2. Open Your PDF Form: Launch WPS PDF and open the interactive form document you wish to edit.
  3. 3. Access Form Controls: Navigate to the 'Form' tab on the top ribbon to view all available form creation and editing tools.
  4. 4. Modify Field Properties: Double-click any text field to access its properties, where you can define formatting, character limits, and validation rules.
Intuitive tools for creating and editing interactive PDF formsExcellent compatibility with standard interactive PDF formats from Adobe Acrobat and Microsoft OfficeSupports extensive form field formatting and properties customizationLightweight, fast, and easy to navigate for users of all skill levels
microsoft office alternative - wps office

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.