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! Link to comment https://forums.phpfreaks.com/topic/59271-canadian-and-usa-zip-code-validation-is-it-possible/ 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 Link to comment https://forums.phpfreaks.com/topic/59271-canadian-and-usa-zip-code-validation-is-it-possible/#findComment-294389 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. Link to comment https://forums.phpfreaks.com/topic/59271-canadian-and-usa-zip-code-validation-is-it-possible/#findComment-294406 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)) { ... Link to comment https://forums.phpfreaks.com/topic/59271-canadian-and-usa-zip-code-validation-is-it-possible/#findComment-294409 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. Link to comment https://forums.phpfreaks.com/topic/59271-canadian-and-usa-zip-code-validation-is-it-possible/#findComment-294532 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 Link to comment https://forums.phpfreaks.com/topic/59271-canadian-and-usa-zip-code-validation-is-it-possible/#findComment-295509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.