PDF Editing Issues

How to Validate US ZIP Codes by State in a PDF Form

Khadija KhanKhadija Khan Nov 2, 2026 869 views

Question details

The user wants to validate five-digit US ZIP codes against state-specific ranges within an interactive PDF form to ensure accurate data entry.

How to Validate US ZIP Codes by State in a PDF Form
Product
Adobe PDF Forms
Device & OS
not provided
Scenario
Creating or editing an interactive PDF form that requires users to input a US ZIP code that matches the previously selected US state.
Observed behavior
Without a validation script, the form allows users to enter any five-digit number, resulting in ZIP codes that do not correspond to the selected state.
Before you start

Ensure you have access to a premium PDF editor that supports JavaScript form editing (such as Adobe Acrobat Pro) and a complete reference list of valid USPS ZIP code ranges for each state.

Solution 1Recommended

Use a Custom JavaScript Validation Script on the ZIP Code Field

Implement a custom validation script that checks the entered ZIP code value against a JavaScript object containing state-specific USPS ZIP code ranges.

This method requires creating a generic JavaScript object that maps state abbreviations to their respective valid ZIP code ranges. By applying this script to the validation properties of your ZIP code text field, you can automatically verify the input against the state selected in another field.

1
Enter form editing mode

Open your PDF form in your PDF editor (like Adobe Acrobat) and access the 'Prepare Form' or form editing mode.

2
Access field properties

Right-click the ZIP Code text field you want to validate and select 'Properties' from the context menu.

3
Set up a custom validation script

Navigate to the 'Validate' tab, select the 'Run custom validation script' option, and click the 'Edit' button.

4
Define the state range object

In the JavaScript editor, define your state ranges using a JavaScript object. For example: var oZipRanges = {'TX': [[73301, 73301], [75001, 75501], [75503, 79999]], 'AL': [[35004, 36925]]};

5
Retrieve and test the values

Add the script to retrieve the selected state from the State field: var zipRange = oZipRanges[this.getField('State').value]; Next, write an 'if' statement to convert the entered ZIP code to a number and test if it falls within the retrieved zipRange array.

6
Add an error alert for missing states

Include an 'else' condition to alert the user if no state is selected yet. Use: app.alert('A state must be selected first.'); event.rc = false; to block the invalid entry.

Use a Custom JavaScript Validation Script on the ZIP Code Field
Maintenance Required: USPS ZIP code ranges occasionally change. You will need to maintain and update the 'oZipRanges' JavaScript object to ensure your form's validation remains perfectly accurate over time.
Seamless PDF Form Management

Easily Manage and Fill PDF Forms with WPS Office

While advanced JavaScript validation logic must be configured in specialized form creators, WPS PDF provides a lightweight, seamless experience for viewing, filling, and editing interactive PDF forms. It ensures your forms retain their strict formatting and validation rules across all your devices.

  1. 1. Install WPS Office: Download and install the free WPS Office suite on your device.
  2. 2. Open your PDF form: Launch WPS PDF and open your interactive PDF document containing the form fields.
  3. 3. Fill out the form: Click on the text fields or dropdown menus to enter your data. Existing validation rules, such as ZIP code checks, will execute as you type.
  4. 4. Save and share securely: Save your completed PDF form locally or share it directly from the WPS interface.
Fully compatible with standard interactive PDF forms and their validation scriptsLightweight application that opens large, complex PDFs quickly without lagFree built-in tools to seamlessly fill forms, sign documents, and add commentsCross-platform support for Windows, Mac, Linux, iOS, and Android
QA img-9

Frequently Asked Questions

Will this JavaScript validation work in free PDF readers?

Most modern desktop PDF readers (like WPS PDF or Adobe Acrobat Reader) support executing standard JavaScript for form validation when a user fills out the form. However, to actually add or edit the underlying script, you typically need a premium PDF editor.

What happens if a user enters a 9-digit ZIP code (ZIP+4)?

The script detailed above focuses on standard 5-digit ZIP codes. To handle ZIP+4 codes, you would need to modify the JavaScript to either strip the last four digits before validation or update your range logic to recognize the expanded format.

Why is the validation script not running when I test the form?

Ensure that JavaScript execution is enabled in your PDF viewer's preferences. In Acrobat, for instance, you can verify this by going to Preferences > JavaScript and ensuring 'Enable Acrobat JavaScript' is checked.

Is there a way to auto-populate the state based on the ZIP code instead?

Yes, but it requires reverse logic. You would write a custom Keystroke or Format script on the ZIP code field that searches your range object and automatically updates the 'State' field's value once a valid 5-digit ZIP code is recognized.