PDF Editing Issues

How to Auto-Populate a State from a City in PDF Forms via JavaScript

Tauseeq MagsiTauseeq Magsi Nov 10, 2026 869 views

Question details

The user needs to automatically fill a 'State' text field based on the value entered in a 'City' field within an interactive PDF form.

How to Auto-Populate State from City in PDF Forms using JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Creating or editing an interactive PDF form where data entry can be streamlined by auto-filling related geographical fields.
Observed behavior
A custom JavaScript validation script must be applied to evaluate the city input upon entry and dynamically output the correct state in a separate field.
Before you start

Ensure you have a PDF editor that supports JavaScript (such as Adobe Acrobat Pro) and that your form already contains separate text fields named exactly as referenced in your script (e.g., 'City' and 'State').

Solution 1Recommended

Add a Custom Validation Script to the City Field

By adding Acrobat JavaScript to the validation tab of the City field, the PDF will automatically evaluate the input and update the State field accordingly.

This method uses an 'if/else if' statement to check the value entered into the City field. When a match is found, the script uses the 'getField' method to target the State field and insert the corresponding value.

1
Open Form Editing Mode

Open your PDF document in Adobe Acrobat and enter 'Prepare Form' mode from the tools menu.

2
Access Field Properties

Right-click on the field where the user will enter the city (the 'City' field) and select 'Properties' from the context menu.

3
Navigate to the Validation Tab

In the Properties dialog box, click on the 'Validate' tab to configure the field's validation behavior.

4
Run Custom Validation Script

Select the 'Run custom validation script' radio button, then click the 'Edit' button next to it.

5
Enter the JavaScript Code

Paste the following script into the editor: if (event.value == "Brooklyn") this.getField("State").value = "New York"; else if (event.value == "Los Angeles") this.getField("State").value = "California"; Adjust the city names, state names, and the exact name of your State field ("State") as necessary. Click 'OK' and close the properties window to test your form.

Add a Custom Validation Script to the City Field
Case Sensitivity: JavaScript is case-sensitive. Ensure that the text users input exactly matches the text in your script (e.g., 'Brooklyn' with a capital 'B'), or consider adding additional JavaScript to convert the input to lowercase before evaluating.
Powerful PDF Editor

Easily Edit and Manage PDF Forms with WPS Office

WPS Office provides a comprehensive PDF toolset that allows you to read, fill, and manage interactive PDF forms effortlessly without needing complex configurations.

  1. 1. Launch WPS Office: Open WPS Office and navigate to the 'PDF' section from the main workspace.
  2. 2. Open Your PDF Form: Click 'Open' and select the interactive PDF form you wish to fill or edit.
  3. 3. Fill or Modify Fields: Use the 'Form' tab to interact with text fields, checkboxes, and drop-down menus seamlessly.
  4. 4. Save and Export: Once your data is entered, click 'Save' or export your document as a flattened PDF if you want to lock the fields.
Supports interactive PDF form filling and editingHighly compatible with standard Adobe Acrobat PDF formatsLightweight software with an intuitive, easy-to-navigate user interfaceAll-in-one suite covering PDF, Word, Excel, and Presentation needs
QA img-9

Frequently Asked Questions

What happens if a user enters a city that is not included in the script?

If the entered city does not match any of the conditions in your 'if/else if' statements, the script will do nothing, and the State field will remain as it was (either blank or containing the previous value). You can add a final 'else' statement to clear the State field if an unrecognized city is entered.

Can I auto-populate multiple fields from a single input?

Yes. Inside your 'if' statement, you can add multiple 'this.getField' commands to populate other fields, such as 'Zip Code' or 'Country', simultaneously based on the single city entry.

Why is my JavaScript validation script not running?

Ensure that JavaScript is enabled in your PDF viewer's preferences. In Acrobat, this is found under Edit > Preferences > JavaScript. Also, double-check that your field names in the script perfectly match the actual field names in the form.

Does this JavaScript work on mobile PDF viewers?

JavaScript support varies heavily across mobile PDF readers. While it works reliably on desktop versions of Adobe Acrobat and other advanced PDF software, many default mobile PDF apps may not execute the validation scripts.