Other PDF Issues

How to Use JavaScript to Map ZIP Codes to Cities in PDF Forms

Algirdas JasaitisAlgirdas Jasaitis Nov 10, 2026 868 views

Question details

The user needs a way to automatically populate city and state fields in a PDF form based on a user-entered ZIP code using JavaScript.

How to Use JavaScript to Map ZIP Codes to Cities in PDF Forms
Product
PDF Forms
Device & OS
not provided
Scenario
Creating an interactive PDF form where entering a ZIP code automatically fills in the corresponding geographic data to save time and reduce errors.
Observed behavior
PDF JavaScript does not have a built-in database to look up ZIP codes automatically, requiring the developer to provide a custom data source or hard-coded object array.
Before you start

Ensure you have a reliable list of the ZIP codes, cities, and states you need to map, and verify that your PDF editor supports creating and executing custom document-level JavaScript.

Solution 1Recommended

Store ZIP Code Mappings in JavaScript Arrays

Embed a structured array or object of ZIP codes directly into your PDF document-level JavaScript to enable instant, offline lookup.

This method is ideal for forms that require a limited number of ZIP codes, such as a regional application. By embedding the data directly into the PDF, the lookup logic works perfectly offline across standard PDF viewer applications without requiring external database connections.

1
Open Form Preparation

Open your PDF document in a professional PDF editor and navigate to the 'Prepare Form' or 'Edit Form' mode to view your interactive fields.

2
Create Document-Level Script

Add a new document-level JavaScript. Define a JavaScript object linking ZIP codes to their respective cities and states. For example: var zipData = { '10001': { city: 'New York', state: 'NY' } };

3
Add Validation Script to ZIP Field

Open the Properties of the 'ZIP Code' text field. Navigate to the 'Calculate' or 'Validate' tab, select 'Run custom validation script', and click Edit.

4
Write the Lookup Logic

In the custom script window, write logic that reads the entered ZIP code, checks it against your zipData object, and populates the target fields using commands like: this.getField('City').value = zipData[event.value].city;

Store ZIP Code Mappings in JavaScript Arrays
Data Limitations: Hard-coding an entire national ZIP code database into your document-level JavaScript will make your PDF file extremely large and slow to process. Use this method primarily for smaller, specific datasets.
Powerful PDF Form Editor

Create and Manage Interactive PDFs with WPS Office

WPS Office includes a comprehensive PDF editor that lets you easily create, format, and manage interactive PDF forms. Whether you need to add text fields, manage form data, or redesign your document layouts, WPS PDF simplifies the entire workflow.

  1. 1. Download and Install: Get the latest version of WPS Office from the official website and launch the WPS PDF application.
  2. 2. Open Your PDF: Click 'Open' to load your existing PDF document or start with a blank page to build a new form.
  3. 3. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access tools for inserting text fields and other interactive elements.
  4. 4. Edit Field Properties: Right-click any form field to adjust its properties, set field names, and format the appearance for your ZIP, City, and State inputs.
Easily add and format text fields, checkboxes, and buttons in PDF formsHighly compatible with standard PDF interactive elements and Adobe AcrobatLightweight application with an intuitive, user-friendly interfaceCross-platform support for editing PDFs on Windows, Mac, and mobile devices
microsoft office alternative - wps office

Frequently Asked Questions

Can a PDF automatically find a city based on a ZIP code without JavaScript?

No, standard PDF forms do not have built-in geocoding or database lookup features. Custom JavaScript and a referenced data source are required to map these values automatically.

Why is my PDF loading slowly after adding a ZIP code array?

If you hard-code a massive database (like the 40,000+ US ZIP codes) directly into the document-level JavaScript, it drastically increases the file size and consumes heavy memory, causing the PDF viewer to lag.

Can I connect my PDF form to an online live ZIP code API?

Security restrictions in most standard PDF viewers block external HTTP API calls by default to prevent malicious activity. You generally need to use an approved local data connection or hard-code a smaller array of needed codes.