PDF Editing Issues

How to Validate a Custom PDF Field Format (e.g., A19-001)

Elise WilliamsElise Williams Nov 3, 2026 870 views

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.

How to Validate a Custom PDF Field Format (e.g., A19-001)
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.
Before you start

Ensure you are using a PDF editor that supports JavaScript for form field validation, and familiarize yourself with basic regular expression (RegEx) syntax.

Solution 1Recommended

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.

1
Open field properties

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

2
Navigate to the Validate tab

In the Text Field Properties dialog box, click on the 'Validate' tab at the top.

3
Enable custom validation

Select the 'Run custom validation script' option, and then click the 'Edit' button.

4
Enter the JavaScript

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

5
Save and test

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.

Apply a Custom Validation Script using JavaScript
Automating Sequential Numbers: This script perfectly validates manual user input but does not automatically generate sequential numbers. If you need auto-incrementing numbers, consider using an interactive button with an incrementing script instead of allowing manual entry.
Edit PDF Forms Easily

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. 1. Open your PDF: Launch WPS Office and open your PDF document.
  2. 2. Access form tools: Navigate to the 'Form' tab in the top ribbon menu.
  3. 3. Insert form controls: Click 'Text Field' or other form controls to insert them into your document workspace.
  4. 4. Customize properties: Double-click the inserted form field to open the Properties window and define its validation rules or appearance.
Easily add and edit interactive PDF form fieldsHighly compatible with Adobe Acrobat PDF forms and featuresLightweight application with a highly intuitive user interfaceCross-platform support for Windows, Mac, and mobile devices
microsoft office alternative - wps office

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.