
The regular expression below tests a French postcode of the form “DDDDD”. Every digit is used. The first two digits are the département number, and run from 01 to 98. The code that follows is the postcode regular expression that checks whether the format is correct.
postcode regular expression
/^(?:0[1-9]|[1-8]\d|9[0-8])\d{3}$/Here are also postcode regular expressions for other countries, just swap the expression for the one you need.
Belgium
Postcodes of the form “DDDD”, the first digit being the province, the others the delivery sectors. Belgian postcodes never start with a zero.
/^(?:(?:[1-9])(?:\d{3}))$/Switzerland
Four digits: the first is the district, the second the zone, the third the route, the fourth the post office number.
/^[1-9]\d{3}$/United Kingdom
Layout of the British postcode
/^(?:GIR 0AA|(?:(?:(?:A[BL]|B[ABDHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGK-PRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?\d|(?:(?:E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(?:SW|W)(?:[2-9]|[1-9]\d)|EC[1-9]\d)\d[ABD-HJLNP-UW-Z]{2}))$/iGermany
Postcodes of the form “DDDDD”. Every digit is used. The first digit is the delivery zone, the second the delivery region. The remaining digits give the delivery district and the postal town.
/^\d{5}$/Spain
Five digits, the first two give the province. Third digit: major city. Last two digits: rural areas.
/^(?:0[1-9]|[1-4]\d|5[0-2])\d{3}$/Italy
First digit: region. Second digit: province. Third digit: capital or province (odd for the capital). Fourth digit: route. Fifth digit: place on the route (0 for small places).
/^\d{5}$/United States
ZIP codes of the form “DDDDD” or “DDDDD-DDDD”. Every digit is used, and none of them carries a particular meaning.
/^(?:GIR 0AA|(?:(?:(?:A[BL]|B[ABDHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGK-PRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?\d|(?:(?:E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(?:SW|W)(?:[2-9]|[1-9]\d)|EC[1-9]\d)\d[ABD-HJLNP-UW-Z]{2}))$/iJavaScript version of the postcode regular expression
The valid postcode format is “DDDDD”. The function takes the postcode as a parameter and matches it against the regular expression. If it finds no match, validation fails and the function returns false, a match returns true.
JavaScript function for a postcode regular expression
/**
* Validate a postal code with a regular expression
*
* If the postal code is not valid, return false
*
* @param postcode
* @return Boolean
*/
function validatePostcode(postcode){
var Reg = new RegExp(/^(?:0[1-9]|[1-8]\d|9[0-8])\d{3}$/i);
return Reg.test(postcode);
}How to use the JavaScript function
The best way to use it is inside an IF statement, as below.
if(validatePostcode(postcode)){
alert("Code postal valide");
} else {
alert("Code postal invalide");
}
See the Pen
Untitled by Damien Flandrin (@dam62500)
on CodePen.
PHP version of the postcode regular expression
Validating postcodes on the front end is not enough: the same check has to run on the server. Here is a PHP snippet that validates postcodes server-side.
PHP function for a postcode regular expression
/*
* validate the postal code
* @param $postcode
*/
function isValidPostcode($postcode)
{
return preg_match('/^(?:0[1-9]|[1-8]\d|9[0-8])\d{3}$/i', $postcode);
}HTML version
In HTML you can build fields that report whether the value is valid through the pattern attribute of <input>, where you can put a postcode regular expression. Be careful: this is only useful for showing the information visually to the user, with red borders for instance.
<input
name="postcode"
type="text"
pattern="(?:0[1-9]|[1-8]\d|9[0-8])\d{3}"
placeholder="DDDDD"
/>
See the Pen
Validating a postcode with a regular expression in HTML by Damien Flandrin (@dam62500)
on CodePen.


