Free tool · runs in your browser
ZIP code regex tester
Test US ZIP, ZIP+4, Canadian, UK and other postal codes against a ready regex, one value per line, then copy the JavaScript regex, the HTML pattern attribute or a Python snippet.
/^\d{5}(-\d{4})?$/
Five digits with an optional hyphen and 4-digit add-on.
2 of 5 pass
- Pass
02134 - Pass
02134-1234 - Fail
2134 - Fail
021341234 - Fail
K1A 0B1
The HTML pattern has no ^ or $: browsers anchor it to the whole value themselves.
Postal code patterns by country
| Country | Regex | Example | Notes |
|---|---|---|---|
| US ZIP (5 digits) | /^\d{5}$/ | 02134 | Five digits. Keep it a string: leading zeros are real (02134 is Boston). |
| US ZIP+4 | /^\d{5}(-\d{4})?$/ | 02134-1234 | Five digits with an optional hyphen and 4-digit add-on. |
| Canada | /^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z] ?\d[ABCEGHJ-NPRSTV-Z]\d$/i | K1A 0B1 | Letter-digit-letter, optional space, digit-letter-digit. D, F, I, O, Q and U are never used, and W and Z never come first. |
| UK | /^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$/i | SW1A 1AA | A practical pattern: it checks the format, not whether the postcode exists. |
| Australia | /^\d{4}$/ | 2000 | Four digits. |
| India (PIN) | /^[1-9]\d{5}$/ | 110001 | Six digits; the first is never 0. |
| Germany | /^\d{5}$/ | 10115 | Five digits, leading zeros allowed (01067 is Dresden). |
The HTML ZIP code input
<label for="zip">ZIP code</label>
<input id="zip" name="zip" inputmode="numeric" autocomplete="postal-code" pattern="\d{5}(-\d{4})?" title="5-digit ZIP or ZIP+4, like 02134 or 02134-1234" required>The pattern attribute has no ^ or $: the browser matches it against the whole value. inputmode="numeric" opens the number keypad on phones, and autocomplete="postal-code" lets autofill fill the field.
Why not type=number?
A number input stores a number, and numbers have no leading zeros: 02134 becomes 2134, which is a different (invalid) ZIP. It also rejects the hyphen in ZIP+4 and adds spinner arrows. ZIP codes are text that happens to contain digits.
Normalise on the server
Browser checks are easy to skip, so check again on the server, and clean the value before you match and store it: trim spaces, uppercase letters, and add the space in Canadian codes so k1a0b1 is stored as K1A 0B1.
function normaliseCanadianPostal(value) {
const v = value.trim().toUpperCase().replace(/\s+/g, "");
return v.length === 6 ? `${v.slice(0, 3)} ${v.slice(3)}` : v; // "k1a0b1" → "K1A 0B1"
}A regex checks format only. It can't tell you that a ZIP or postcode exists or matches the street address; that needs an address lookup.
ZIP code regex FAQ
What is the regex for a US ZIP code?
Use ^\d{5}$ for a 5-digit ZIP and ^\d{5}(-\d{4})?$ to also accept ZIP+4, such as 02134-1234. In the HTML pattern attribute, leave off ^ and $, because browsers anchor the pattern to the whole value.
What is the regex for a Canadian postal code?
^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z] ?\d[ABCEGHJ-NPRSTV-Z]\d$ with the case-insensitive flag. It accepts K1A 0B1 and k1a0b1, and rejects codes with D, F, I, O, Q or U, which are never used, or with W or Z as the first letter.
Why not use type=number for a ZIP code?
type=number treats the value as a number, so leading zeros are lost: 02134 becomes 2134. It also adds spinner arrows and rejects the hyphen in ZIP+4. Use a text input with inputmode="numeric" for the number keypad instead.
Does a ZIP code regex check that the code exists?
No. A regex checks the format only. 99999 passes the US pattern but is not a real ZIP. To confirm a code exists, check it against a postal address lookup service on the server.
Is there one regex for every country's postal code?
No. Formats differ by country: four digits in Australia, six in India, letters and digits in Canada and the UK, and some countries have no postal codes at all. Ask for the country first, then apply that country's pattern.
Regex patterns for form fields
Alphanumeric regex tester
Test letters and numbers, spaces, handles and Unicode patterns.
Email regex tester
Test email patterns against edge cases and your own list.
Phone number regex tester
Check numbers against E.164, US/Canada and digits-only patterns.
Password regex generator
Pick strength rules and copy the regex or HTML pattern.
Number regex tester
Test digits, decimals, money and thousands separators.
Date regex tester
Date formats, strict ranges and leap-year aware ISO patterns.
Name regex tester
Validate names with accents, hyphens and apostrophes.
Username regex tester
Test username length, separators and reserved names.
IP address regex tester
Validate IPv4, IPv6 and CIDR input with strict octets.
URL regex tester
Check website fields with regex, type=url or new URL().
HTML pattern attribute
How browsers anchor and check a pattern on an input.
HTML required attribute
Stop a form from sending while a field is empty.
Validate a form with JavaScript
Use checkValidity, custom messages and your own rules.
Time regex tester
Validate 24-hour, AM/PM and ISO 8601 time formats.
Credit card regex tester
Match card brands by prefix and length, then run a Luhn check.
Hex color regex tester
Match hex, rgb() and hsl() colors with range checks and swatches.
UUID regex tester
Match UUID v4, v7 and GUIDs with version and variant checks.
Use your validation patterns as you assemble a form with our HTML contact form code or HTML form generator.
More free form tools
Form backend cost calculator
Enter monthly submissions and compare the cheapest listed plan across eight form backends.
Email deliverability checker
Enter a domain to check its SPF, DMARC and MX records and get a plain-English fix for each.
SPF record checker
Look up a domain's SPF record, see each mechanism explained and count DNS lookups against the 10-lookup limit.
DMARC record generator
Pick a policy and report address and copy a ready _dmarc TXT record.
DMARC checker
Look up a domain's DMARC record and see the policy and every tag explained.
DKIM record checker
Enter a domain and selector to check its DKIM key, with presets for common senders.
SPF record generator
Tick your email providers and copy one SPF TXT record with a lookup count.
Contact form spam checker
Paste form HTML to audit its honeypot, endpoint, CAPTCHA markers and spam resistance.
Honeypot field generator
Generate a hidden anti-spam input with your chosen name and hiding technique.
Form action URL tester
Check a form action URL for reachability, CORS headers, POST acceptance and backend.
Email obfuscator
Encode your email so spam scrapers can't read it but people can still click it.
Mailto link generator
Build a mailto: URL with recipients, CC, BCC, subject and a pre-filled body.
HMAC-SHA256 generator
Generate and verify webhook signatures for common providers in your browser.
URL encoder and decoder
Encode text, build an x-www-form-urlencoded body or parse a query string.
UTM builder
Build campaign URLs with UTM parameters, tag a CSV in bulk and check tagged links.
HTML to JSX converter
Paste HTML and get JSX or a working React/Next.js form component.
Image to Base64 converter
Convert images to Base64 data URLs in your browser and copy HTML and CSS snippets.
Base64 to image converter
Paste a Base64 data URI to preview and download the decoded image.
Base64 decode & encode
Decode Base64 to readable UTF-8 text or encode text to Base64, locally in your browser.
URL to Base64 converter
Fetch a direct image URL in your browser and get its Base64 data URL.
AI form generator
Describe your form in plain English and copy working HTML.