Canada’s postal code validation in PHP

Posted on March 24, 2008 
Filed Under php, tutorial

Few days back, My friend Narendra asked me how to validate the postal code of Canada which is entered through the text box.And, I came up with this Php function as solution for him.

 

These are the conditions for validation of the postal code of Canada.

  1. There are six alphanumeric letters.
  2. Alphabate and digits are alternate and first letter must be alphabate.
  3. The alphabate shouldn’t contain letters “d”,”f”,”i”,”o”,”q” and “u”.

Postal code “A1B2c5” is a valid postal code whereas “A1f2cZ” or “1AC2E5” are invalid postal code of Canada.

//function to validate postal code of canada
function validateCanadaZip($zip_code)
{
 //function by Roshan Bhattara(http://roshanbh.com.np)
 if(preg_match("/^([a-ceghj-npr-tv-z]){1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}$/i",$zip_code))
    return true;
 else
    return false;
}

Now, let’s look at the explanation of the regular expression inside the preg_match() to validate the postal code of Canada as specified by the above condition. As you might know that a Perl compatible regular expression enclosed within “/” sign. The “i” at the end of the expression refers that this comparison is case insensitive.As most of you know, “^”sign is the start of the regular expression and “$” represent the end of the regular expression. And there are three repeated expressions like “([a-ceghj-npr-tv-z]){1}[0-9]{1}”. Let me explain it, the expression “([a-ceghj-npr-tv-z]){1}” represents that there should be one alphabate which doesn’t contain “d”,”f”,”i”,”o”,”q” and “i”. Furthermore, the expression “[0-9]{1}” represents that there should be one digit.

Popularity: 9% [?]

Enter your email address and get recent tutorials, tips, tricks and scripts of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Follow me on twitter at http://twitter.com/roshanbh.

Related Posts

» Php function to validate two decimal places of a number
» USA’s phone number validation using Regular expression in PHP
» Date format validation in PHP
» USA’s Zip Code Validation in PHP

Comments

6 Responses to “Canada’s postal code validation in PHP”

  1. PHP Coding School » Blog Archive » php code [2008-03-24 16:59:21] on March 24th, 2008 5:06 pm

    [...] Canada’s postal code validation in PHP By Roshan Yesterday, My friend Sushil asked me how to validate the postal code of Canada which is entered through the text box.And, I came up with this Php function as solution for him. - http://roshanbh.com.np [...]

  2. Matt on March 26th, 2008 6:33 am

    I’m confused: “Postal code “A1B2cZ” is a valid postal code” …

    “Z” != [0-9]{1}

    furthermore, that’s not “alternating”…. the “Z” should be numeric per the rule #2, no?? “cZ” does not alternate between numeric/alpha… Just a point of curiosity more than anything.

  3. Roshan on March 26th, 2008 6:54 am

    sorry for the confustion..that was mistakenly typed and corrected…..thanks a lott

  4. rakesh on June 18th, 2008 10:55 am

    I am using PHP and Mysql(database ) I want to send bulk mail but my server not support .. my server guy told send bulk mail using your localhost … can any one help me.how can i send bulk mail ….. ?????

  5. Mr. Gift Baskets on August 8th, 2008 1:45 pm

    Hi Roshan, thank you for the great piece of code. One minor issue, the code requires 6 continuous characters, but many people put a space in the middle to make it A2A 2A2 instead of A2A2A2. Some international visitors might also do A2A2 A2 if that’s the format of their own country’s postal code. How can I allow for any spaces between characters?

    The Gift Basket King :) (a.k.a James)

  6. KranF.com - Le Blogue » Archive du blogue » Valider un code postal Canadien en PHP on August 28th, 2008 10:13 pm

    [...] Pour ce faire j’ai fait une recherche rapide sur Google qui m’a permis de découvrir une solution toute préparée à l’avance! [...]

Leave a Reply