How to Use JavaScript to Map ZIP Codes to Cities in PDF Forms
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.

- 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.
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.
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.
Open your PDF document in a professional PDF editor and navigate to the 'Prepare Form' or 'Edit Form' mode to view your interactive fields.
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' } };
Open the Properties of the 'ZIP Code' text field. Navigate to the 'Calculate' or 'Validate' tab, select 'Run custom validation script', and click Edit.
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;

Load ZIP Code Mappings from an External Data Source
Use an external text file or database to look up ZIP codes, keeping the PDF file size lightweight while supporting massive datasets.
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. Download and Install: Get the latest version of WPS Office from the official website and launch the WPS PDF application.
- 2. Open Your PDF: Click 'Open' to load your existing PDF document or start with a blank page to build a new form.
- 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. 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.

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.




