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

PartMeaning
^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

InputMatch?Note
90210YesValid format
10001YesValid format
94105-1234YesValid format
1234NoOnly 4 digits (need exactly 5)
123456No6 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

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.

Built by Michael Lip. 100% client-side — no data leaves your browser.