tqla Posted July 10, 2007 Share Posted July 10, 2007 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! Quote Link to comment Share on other sites More sharing options...
per1os Posted July 10, 2007 Share Posted July 10, 2007 ereg("^[A-Z]{1}[0-9]{1}[A-Z]{1}-[0-9]{1}[A-Z]{1}[0-9]{1}", $zip) Not sure if that will work, but yea you want to look at regular expressions www.php.net/ereg Quote Link to comment Share on other sites More sharing options...
effigy Posted July 10, 2007 Share Posted July 10, 2007 <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. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 10, 2007 Share Posted July 10, 2007 Either of these (includes Canadian, US 5 digit, US 9 digit): if (preg_match('/([A-Z]\d){3}|(\d{5}(-\d{4})?)/i', $zip) { ... // OR if (preg_match('/(?:[A-Z]\d){3}/i', $zip) or preg_match('/\d{5}(?:-\d{4})?/', $zip)) { ... Quote Link to comment Share on other sites More sharing options...
per1os Posted July 10, 2007 Share Posted July 10, 2007 <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. Quote Link to comment Share on other sites More sharing options...
OLG Posted July 11, 2007 Share Posted July 11, 2007 Or else, Simply get a lovely piece of software called Regexbuddy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.