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.