gettoolz.app

US ZIP code regex

Five digits, optionally followed by a hyphen and the four-digit ZIP+4 extension. Copy the pattern, with 4 inputs it accepts and 5 it rejects, each checked against it at build time.

Part of developer tools.

Pattern^\d{5}(?:-\d{4})?$
JavaScript literal/^\d{5}(?:-\d{4})?$/

What the us zip code regex accepts and rejects

Each result below is the output of the pattern itself, run on the input when this page was built — not a claim copied next to it. A wrong verdict stops the build.

Inputs tested against the US ZIP code regex
InputResultWhy
90210Passesa five-digit ZIP
10001-1234PassesZIP+4
02134Passesa leading zero, common in New England
9021Failsfour digits
902101Failssix digits
90210-12Failsa two-digit extension
90210 1234Failsa space instead of the hyphen
SW1A 1AAFailsa UK postcode

What it wrongly lets through

These inputs match although they are not valid — checked the same way, so this list is as true as the one above.

Inputs the US ZIP code regex wrongly accepts
InputResultWhy
00000Passesno ZIP code is 00000

Limits

  • Any five digits pass, allocated or not. Only the USPS database knows which codes exist.
  • Store ZIP codes as text, never as numbers: a numeric column silently turns 02134 into 2134.
  • Other countries have their own formats — UK postcodes, Canadian postal codes, French five-digit codes that look like ZIPs but are not.

Try it on your own input

Open this pattern in the regex tester with every example above on its own line. The link adds the g and m flags so that ^ and $ hold per line and every line is tested: the highlighted lines are the ones marked as passing here. Paste your own data in place of the examples — it is matched in your browser and never uploaded.

Other common regexes

Or open the regex tester empty and bring your own pattern.