What Is a Good Regex for US ZIP Code Validation?
Pattern: ^\d{5}(-\d{4})?$. US ZIP codes are either 5 digits (ZIP) or 9 digits in ZIP+4 format (12345-6789). The first digit indicates the region (0 = Northeast, 9 = West Coast). While this regex validates the format, only the USPS database can confirm a ZIP code actually exists.
Breaking Down the Pattern
| Part | Meaning |
|---|---|
^ | Start of string |
\d{5} | Five digits (basic ZIP code) |
(-\d{4})? | Optional: hyphen followed by 4 digits (ZIP+4) |
$ | End of string |
Test Cases
| Input | Match? | Note |
|---|---|---|
90210 | Yes | Valid format |
10001 | Yes | Valid format |
94105-1234 | Yes | Valid format |
1234 | No | Only 4 digits (need exactly 5) |
123456 | No | 6 digits without the +4 format |
Usage Examples
JavaScript
const pattern = /^\d{5}(-\d{4})?$/;
pattern.test('90210'); // true
pattern.test('1234'); // false
Python
import re
pattern = r'^\d{5}(-\d{4})?$'
bool(re.match(pattern, '90210')) # True
bool(re.match(pattern, '1234')) # False
Common Pitfalls
- This validates the format but not whether the ZIP code actually exists. ZIP 00000 passes but is invalid.
- Some ZIP codes start with 0 (e.g., 01001 in Massachusetts) -- never store them as numbers.
- US territories (PR, GU, VI) have valid ZIP codes that follow the same format.
Try It Yourself
Test this regex with our Regex Tester.
Frequently Asked Questions
What is ZIP+4?
ZIP+4 adds a hyphen and 4 more digits to the basic ZIP code, identifying a specific delivery segment (block, building, or PO box). It is optional for addressing but improves mail sorting.
Do ZIP codes ever start with zero?
Yes. ZIP codes in the northeastern US (CT, MA, ME, NH, NJ, NY, PR, RI, VT, VI) start with 0. Always store ZIP codes as strings, never as integers.
Is this regex valid for international postal codes?
No. Other countries use completely different formats. UK postcodes have letters, Canadian codes alternate letters and digits, and many countries have 4 or 6-digit codes.