Jump to content

Canadian and usa zip code validation. Is it possible?


tqla

Recommended Posts

Hi. I am using this code to validate USA zip codes. I want to make it also accept and validate Canadian zip codes. Canadian zip codes are in this format: L2K-6T2 (alpha, num, alpha - num, alpha, num). Can someone show me how modify this code to do this?

 

    /* check zip for invalid format. */
    elseif ($field == "zip")
    {
      if(!ereg("^[0-9]{5,5}(\-[0-9]{4,4})?$",$value))
      {
           $bad_format[] = $field;
      }

 

Thank you!

<pre>
<?php
echo preg_match('/\A[A-Z]\d[A-Z]-\d[A-Z]\d\z/', 'L2K-6T2');
?>
</pre>

 

frost:

 

Character classes are assumed to be one character by default, so the intervals are not needed.

 

Thanks for straightening me out =) One of these days I will sit down and write a ton of regex's to become an expert at them...one of these days.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.