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!

Link to comment
Share on other sites

<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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.