splitforms.com

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

  • Pass02134
  • Pass02134-1234
  • Fail2134
  • Fail021341234
  • FailK1A 0B1

The HTML pattern has no ^ or $: browsers anchor it to the whole value themselves.

Postal code patterns by country

CountryRegexExampleNotes
US ZIP (5 digits)/^\d{5}$/02134Five digits. Keep it a string: leading zeros are real (02134 is Boston).
US ZIP+4/^\d{5}(-\d{4})?$/02134-1234Five digits with an optional hyphen and 4-digit add-on.
Canada/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z] ?\d[ABCEGHJ-NPRSTV-Z]\d$/iK1A 0B1Letter-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}$/iSW1A 1AAA practical pattern: it checks the format, not whether the postcode exists.
Australia/^\d{4}$/2000Four digits.
India (PIN)/^[1-9]\d{5}$/110001Six digits; the first is never 0.
Germany/^\d{5}$/10115Five digits, leading zeros allowed (01067 is Dresden).

The HTML ZIP code input

zip.html

<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.

normalise.js

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.

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.

See all free tools