Address Form Validation Rules for Web Developers

Good address validation stops obvious mistakes without rejecting real addresses. Too strict and customers cannot check out. Too loose and you ship to addresses that do not exist.

Required and optional fields

Require street line 1, city, state and ZIP code. Keep line 2 for apartments and suites optional. Keep the recipient name separate from the address itself.

ZIP code pattern

Accept five digits, or five digits plus a hyphen and four digits: ^\d{5}(-\d{4})?$. Store the value as text so leading zeros survive.

State field

Use a dropdown with the 50 states and the District of Columbia, plus territories and military codes (AA, AE, AP) if you ship there. A dropdown removes typos and inconsistent spellings.

Cross-field checks

The first three digits of a ZIP code fall within a known range for each state. A lightweight check can warn when a Texas address has a ZIP starting with 9. Present it as a warning rather than a hard block, since data can be out of date.

Length limits

Carrier labels often allow 35 characters per address line. If your limit is shorter, test with long street names and a unit designator to see what gets cut off.

Error messages that help

Say what is wrong and how to fix it: "Enter a 5-digit ZIP code, like 30301" is better than "Invalid input". Keep the entered value in the field so the user does not have to start over.

Test with variety

Generate addresses from several states, including ones with leading-zero ZIPs, long city names and units. Paste them into your form and confirm each one saves, displays and exports correctly.

Generate realistic US addresses for forms, databases and design mockups, then copy or download them as CSV or JSON.

Open the address generator