QA Guide
How to Validate U.S. ZIP Codes & Preserve Leading Zeros
A ZIP Code can pass validation and still lose its first digit on the way to your database or spreadsheet. Validate its format and keep it as text from form entry through export.
Regex patterns for five-digit ZIP Codes and ZIP+4
A standard ZIP Code has five digits. ZIP+4 adds a hyphen and four digits. Choose the JavaScript regex that matches what your form accepts. The ZIP+4 below is a format example for testing.
| Rule | JavaScript regex | Example |
|---|---|---|
| Five digits only | /^[0-9]{5}$/ | 02108 |
| ZIP+4 only | /^[0-9]{5}-[0-9]{4}$/ | 02108-1234 |
| Either format | /^[0-9]{5}(?:-[0-9]{4})?$/ | 02108 or 02108-1234 |
If your form tolerates surrounding spaces, trim the string before validating it. Test that 2108, 02108-123, and 02108 1234 fail the either-format pattern. Apply the same format rule on the server when the form submits to an API.
A regex checks the characters and length. Confirming that a ZIP Code matches a city or street address requires a lookup. See USPS ZIP Code basics.
Why integer fields strip leading zeros
An integer stores a numeric value, so 02108 becomes 2108. Use a text input, send a JSON string, and store it in a text column. A text input can still request a numeric keyboard for a five-digit-only form.
| State | ZIP as text | As an integer |
|---|---|---|
| New Jersey | 08608 | 8608 |
| Massachusetts | 02108 | 2108 |
| Vermont | 05401 | 5401 |
Check the value after saving and loading the record. For example, your API should return {"zip_code":"02108"} so downstream tools receive all five characters.
Preserve ZIP Codes when exporting CSV and opening in Excel
- Export the original string. Open the CSV in a text editor and confirm that the ZIP field still contains
02108. - Import the CSV through Excel's Data tab. Choose Data → From Text/CSV → Transform Data. Set the ZIP column to Text before loading. If Power Query has already added a step that converts the column to a number, remove or edit that step before loading.
- Check the round trip. If you save another CSV from Excel, inspect that file in a text editor too. Confirm that it still contains the original five digits and any ZIP+4 suffix your workflow supports.
Quoting a value in CSV leaves type detection to the importer. If you already opened a CSV and Excel dropped the zeros, reimport the original file as text if its raw values are still intact. See Microsoft's guide to keeping leading zeros.
Try leading-zero ZIP Codes in your test form
Start with the Massachusetts Address Generator or the New Jersey Address Generator. For a repeatable regression test, use a fixed ZIP value from the table and check it after submission, retrieval, and export.
For tests that also cover street lines and units, read the U.S. Address Format Guide for QA & Form Testing.