Pixel Envision Ltd.
Mobile Game Development Studio
  • Home
  • About Us
  • Our Games
    • Hyper-Casual
    • Casual Games
      • Don’t Get Caught
      • Hordes of Enemies
      • noded
      • kubic
      • Whip Swing
      • Witches’ Brew
    • Kids Apps
      • Coloring Book
      • Cars & Trucks Puzzle
      • Train Puzzles for Kids
      • Animal Puzzle
      • Fairy Tale Puzzles for Kids
      • Find the Differences
  • Support
  • Privacy Policy
Select Page ...

ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries

ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries

August 14, 2011 PHP, Programming

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.

License

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!

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
$country_code = "US";
$zip_postal = "11111";
 
$ZIPREG = array(
    "US" => "^\d{5}([\-]?\d{4})?$",
    "UK" => "^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$",
    "DE" => "\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b",
    "CA" => "^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$",
    "FR" => "^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$",
    "IT" => "^(V-|I-)?[0-9]{5}$",
    "AU" => "^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$",
    "NL" => "^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$",
    "ES" => "^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$",
    "DK" => "^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$",
    "SE" => "^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$",
    "BE" => "^[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
    
}
?>

← Witches’ Brew HD Teaser Trailer
PHP e-mail validation code snippet →

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

  • Yogesh Singh
    12 / 12 / 2018

    Thank You
    Regex working properly

    Yogesh Singh 12 / 12 / 2018
  • Rakesh Vishnoi
    2 / 23 / 2018

    Thank you my brother :)

    Rakesh Vishnoi 2 / 23 / 2018
  • Urvisha
    7 / 30 / 2015

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

    Urvisha 7 / 30 / 2015
  • Nate
    6 / 11 / 2015

    Thank you! this is awesome!

    Nate 6 / 11 / 2015
  • Geman
    1 / 30 / 2015

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

    Geman 1 / 30 / 2015
    • Erdener Gonenc
      1 / 31 / 2015

      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.

      Erdener Gonenc 1 / 31 / 2015
  • Menno van der Krift
    10 / 31 / 2014

    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 :D

    Menno van der Krift 10 / 31 / 2014
  • Stéphane
    1 / 13 / 2014

    {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…

    Stéphane 1 / 13 / 2014
    • John
      5 / 15 / 2014

      This is incorrect.
      US formula does not accept 12345-
      In other words it works.

      John 5 / 15 / 2014
  • Mogsdad
    9 / 27 / 2013

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

    Mogsdad 9 / 27 / 2013
  • CNG2013
    6 / 3 / 2013

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

    CNG2013 6 / 3 / 2013
    • Erdener
      6 / 3 / 2013

      No, that’s for generic validity check.

      Erdener 6 / 3 / 2013
  • Tags

    3ds Max Coming Soon CoronaSDK Featured Flash Lua MAXScript PHP Programming Reviews Tips & Tricks Unity 3D Windows Phone
  • Recent Comments

    • Yogesh Singh on ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries
    • Admin on Maxscript – Vray Cubemap Generator for Unity
    • charlie on Maxscript – Vray Cubemap Generator for Unity
    • Mastan on PHP Currency Converter
    • Rakesh Vishnoi on ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries
    • Find us on

      amazonandroidapplefacebooklinkedintwitterwindowsyoutube
    • Company Information

      Lytchett House, 13 Freeland Park, Wareham Road, Poole, Dorset, BH16 6FA

      Pixel Envision Limited is a company registered in England, company number: 09558675. Registered Office: Preston Park House, South Road, Brighton, East Sussex, BN1 6SB, United Kingdom

    • Privacy Policy
    Copyright © 2011-2021 Pixel Envision Ltd, all rights reserved.