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.
^\d{5}(?:-\d{4})?$/^\d{5}(?:-\d{4})?$/Flags: none
Open in the testerWhat 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.
| Input | Result | Why |
|---|---|---|
| 90210 | Passes | a five-digit ZIP |
| 10001-1234 | Passes | ZIP+4 |
| 02134 | Passes | a leading zero, common in New England |
| 9021 | Fails | four digits |
| 902101 | Fails | six digits |
| 90210-12 | Fails | a two-digit extension |
| 90210 1234 | Fails | a space instead of the hyphen |
| SW1A 1AA | Fails | a 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.
| Input | Result | Why |
|---|---|---|
| 00000 | Passes | no 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
- Email address
- URL
- UUID
- IPv4 address
- Hex color
- ISO 8601 date
- 24-hour time
- Semantic version
- URL slug
- Phone number (E.164)
- MAC address
Or open the regex tester empty and bring your own pattern.