For the last couple of years, I’ve been mostly doing PHP coding. As a result, I have small library of code snippets of my own. Those are simple but useful PHP codes that might save some time on your work!

First one I’m sharing here today is the combination of regular expression and the PHP code to do ZIP code (or Postal Code) validation for following 12 countries: United States of America, United Kingdom, Germany, Canada, France, Italy, Australia, Netherlands, Spain, Denmark, Sweden & Belgium.

Code is provided in PHP but the REGEX codes are universal and those can be used with other programming languages.


This code is free to use, distribute, modify and study. When referencing please link back to this website / post in any way e.g. direct link, credits etc. If you find this useful, please leave a comment and share using the buttons below!

	"UK"=>"^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
	"DK"=>"^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$",
if ($ZIPREG[$country_code]) {
	if (!preg_match("/".$ZIPREG[$country_code]."/i",$zip_postal)){
		//Validation failed, provided zip/postal code is not valid.
	} else {
		//Validation passed, provided zip/postal code is valid.
} else {
	//Validation not available

10 Responses to “ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries”

Add Comment
  1. Thanks a lot. Saved my lots of time!!!! :)

  2. Thank you! this is awesome!

  3. Why does Italy has the V- prefix to the postal code? Isn’t neither the CEPT prefix nor the ISO one. Thanks

    • I ended up adding it based on my research at the time of coding, which was 7-8 years ago. As far as I remember, I stands for Italy & V stands for Vatican.

  4. Cheers. Just needed a Dutch zipcode validation for my school project. Took me a while, but i understand what it says in code now and adjusted it a bit for my personal needs.
    Thanks for sharing the code 😀

  5. {1] is useless…

    and it depends of the data you read ^blablabla$
    is useless too.

    US formula is also accepting 12345-

    Did not check more than a quick shot…

  6. This `if ($ZIPREG[$country_code])` will generate a ‘Notice’ if an unsupported country is provided. Better to use `if (isset($ZIPREG[$country_code])` instead.

  7. Will this work for: “enter your zip to see if we deliver to your area”

Leave a Reply