PatternRegex
Validate a US ZIP code
Accept a standard 5-digit US ZIP code or the extended ZIP+4 form, and reject anything that isn't one of those two shapes.
Last updated
Matches
- 90210
- 12345-6789
Doesn’t match
- 1234
- 123456
Why it’s written this way
\d{5} matches the base 5-digit ZIP code every US address needs, and the optional non-capturing group (?:-\d{4})? adds support for the ZIP+4 extension — a dash followed by four more digits — without requiring it.
Because the extension is wrapped in a '?', both '90210' and '12345-6789' satisfy the same anchored pattern, which is the pair of formats USPS actually uses on mail and shipping forms.
Edge cases to know
- →It checks shape only — '00000' and '99999' both match even though neither is an assigned ZIP code.
- →It doesn't know which ZIP codes exist or which state or city they belong to; matching that up needs an actual USPS ZIP database.
- →Some address forms accept a ZIP+4 typed with a space instead of a dash ('12345 6789') — this pattern doesn't, so normalize the separator first if your form allows that.