Back to the tool

Regex for Postal Code Validation: One Pattern Per Country, or Nothing

Regex for postal code validation is a trap with a friendly face. The US pattern takes ten seconds to write, and it works, so the temptation is to write one pattern for every country and call the form done. That pattern will reject real customers in Canada, accept nonsense in the UK, and wave through codes that do not exist anywhere. Postal codes are country-shaped. Your validation has to be too.

Start with the easy one. US ZIP: ^[0-9]{5}(?:-[0-9]{4})?$. Five digits, optional hyphen plus four more. It accepts 90210 and 90210-1234, rejects 9021, 902101, and 90210-123. Anchor both ends. Without ^ and $, a match on 90210 inside a longer string passes, and your validation is decorative. This is the one pattern most tutorials get right, and it is also where most developers stop.

Regex for postal code validation: Canada is where the naive pattern dies

Canadian postal codes alternate letter-digit-letter, space, digit-letter-digit: K1A 0B1. The loose pattern ^[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d$ accepts the shape, including codes Canada Post would never issue. The stricter version: ^(?!.*[DFIOQU])[A-VXY][0-9][A-Z] ?[0-9][A-Z][0-9]$. The lookahead bans D, F, I, O, Q, U anywhere, because those letters never appear in Canadian codes, too easy to confuse. The first character class bans W and Z. The space is optional, so K1A0B1 passes too.

Compare the two approaches. The US pattern is permissive because US ZIPs have no letter rules to enforce. Any five digits is at least a plausible shape. The Canadian pattern is strict because the format itself carries rules about which letters can appear where. Same job, opposite strictness, and the difference comes from the postal system, not from regex style. Copying the US approach to Canada leaves the letter rules unenforced. Copying the Canadian approach to the US invents rules that do not exist.

The UK, the fallback, and the honest limit

UK outward and inward: ^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$ covers the common shapes, SW1A 1AA, M1 1AA, though the full UK rules have more edge cases than a single readable pattern should carry. Normalize first: uppercase, collapse whitespace, trim. A lowercase sw1a 1aa is a real code typed casually, not an invalid one. Validate after normalizing, not before.

For countries you do not have a pattern for, the fallback ^[A-Za-z0-9- ]{2,10}$ keeps the form from rejecting real input while catching empty strings and keyboard smashes. It is a screen door, not a lock, and you should know which one you installed.

The honest limit: regex checks shape, never existence. 90210 is real. 99999 matches the US pattern and is not a deliverable ZIP. A regex cannot know the difference. Real validation pairs the pattern with a lookup against a postal database or an address verification service. The pattern catches typos at the keyboard. The lookup catches inventions. Use both, and say which one you are doing when the form rejects someone.

Before any of this ships, test it the boring way. Build two lists per country: codes that must pass, including edge cases like the optional space in Canadian codes and the ZIP+4 hyphen, and codes that must fail, including wrong lengths, banned letters, and the empty string. Run every pattern against both lists in the actual engine you deploy to, because regex flavors differ on lookaheads and character classes. A pattern that passes in Python and fails in a browser form validator is a bug you wrote yourself, and the two-list test catches it in a minute.

I keep a per-country map in code, keyed by the country dropdown, with the fallback for everything else. The day a new market opens, the map gets a new entry, not a new if-statement. What I have not settled to my satisfaction is how much strictness to apply at signup versus checkout. Rejecting a real customer costs more than accepting a typo, and that tradeoff deserves its own decision, not a default. To adapt these patterns to your own fields and engines, customize a regex template here and test it against your own inputs before shipping.

Frequently asked questions

What is the regex for US ZIP codes?

Five digits with an optional ZIP+4: ^[0-9]{5}(?:-[0-9]{4})?$. Anchor both ends, or a match inside a longer string will pass and your validation is decorative.

Why does my postal code regex reject valid Canadian codes?

Usually the space or the letter rules. Canadian codes exclude D, F, I, O, Q, and U entirely, and the first character excludes W and Z. Make the space optional and add the lookahead that enforces the exclusions.

Should I use one regex for all countries?

No. Keep a per-country map keyed by the country dropdown, with a loose fallback for countries you have no pattern for. One global pattern either rejects real customers or accepts nonsense.

Does regex prove a postal code is real?

No. Regex checks shape only. A code can match the pattern and still not exist. Existence needs a lookup against a postal database or an address verification service.

Related reading: Why Most Email Regex Patterns Are Wrong (And What to Use Instead) · Phone Number Regex: Why You Should Not Write Your Own · URL Validation Regex: Why Most Patterns Are Wrong · Username Validation Regex: The Pattern That Rejects the Right Things

Try the tool

Build and test the username pattern against your own inputs. Build a regex pattern with plain-English explanations.

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.