[PHP] Validating postal codes
Hi, I'm trying to validate a Canadian postal code. I've already written a function; code as follows: if(!eregi('(^[a-z][0-9][a-z][0-9][a-z][0-9]$)',$form[authpstcode])) { $errors[] = 'You must input a valid Canadian postal code (A9A9A9) postal code if he/she resides in Canada'; $continue = false; } The above works OK if the user enters -- M2M6N6 But fails if the user enters --- M2M 6N6 (note the space between the two triplets) I want my function to validate either, i.e., allow a space between the two triplets but not enforce it. Any idea on how to modify my test? Thanks, Don -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating postal codes
This is a RTFM situation!!! instead of just $form[autopstcode] - use str_replace( ,,$form[autopstcode]) (e.g. strip the spaces before validating the triplets) found in 30 seconds by typing string on PHP site. -Original Message- From: DonPro [mailto:donpro;lclcan.com] Sent: 15 November 2002 14:45 To: php list Subject: [PHP] Validating postal codes Hi, I'm trying to validate a Canadian postal code. I've already written a function; code as follows: if(!eregi('(^[a-z][0-9][a-z][0-9][a-z][0-9]$)',$form[authpstcode])) { $errors[] = 'You must input a valid Canadian postal code (A9A9A9) postal code if he/she resides in Canada'; $continue = false; } The above works OK if the user enters -- M2M6N6 But fails if the user enters --- M2M 6N6 (note the space between the two triplets) I want my function to validate either, i.e., allow a space between the two triplets but not enforce it. Any idea on how to modify my test? Thanks, Don -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php _ This message has been checked for all known viruses by the MessageLabs Virus Control Centre. This message has been checked for all known viruses by the MessageLabs Virus Control Centre. * Notice: This email is confidential and may contain copyright material of Ocado Limited (the Company). Opinions and views expressed in this message may not necessarily reflect the opinions and views of the Company. If you are not the intended recipient, please notify us immediately and delete all copies of this message. Please note that it is your responsibility to scan this message for viruses. Company reg. no. 3875000. Swallowdale Lane, Hemel Hempstead HP2 7PY * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating postal codes
This might work: if(!eregi('(^[a-z][0-9][a-z] ?[0-9][a-z][0-9]$)',$form[authpstcode])) { $errors[] = 'You must input a valid Canadian postal code (A9A9A9) postal code if he/she resides in Canada'; $continue = false; } Marco -- php|architect - The magazine for PHP Professionals The monthly worldwide magazine dedicated to PHP programmers Come visit us at http://www.phparch.com! On Fri, 2002-11-15 at 09:44, DonPro wrote: Hi, I'm trying to validate a Canadian postal code. I've already written a function; code as follows: if(!eregi('(^[a-z][0-9][a-z][0-9][a-z][0-9]$)',$form[authpstcode])) { $errors[] = 'You must input a valid Canadian postal code (A9A9A9) postal code if he/she resides in Canada'; $continue = false; } The above works OK if the user enters -- M2M6N6 But fails if the user enters --- M2M 6N6 (note the space between the two triplets) I want my function to validate either, i.e., allow a space between the two triplets but not enforce it. Any idea on how to modify my test? Thanks, Don -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating postal codes
Either you may remove spaces: $form[authpstcode]=str_replace(' ','',$form[authpstcode]) and then compare or change your expresion to '(^\s*[a-z]\s*[0-9]\s*[a-z]\s*[0-9]\s*[a-z]\s*[0-9]\s*$)' The first is better as you may put it into database column that is just char(6) DonPro wrote: Hi, I'm trying to validate a Canadian postal code. I've already written a function; code as follows: if(!eregi('(^[a-z][0-9][a-z][0-9][a-z][0-9]$)',$form[authpstcode])) { $errors[] = 'You must input a valid Canadian postal code (A9A9A9) postal code if he/she resides in Canada'; $continue = false; } The above works OK if the user enters -- M2M6N6 But fails if the user enters --- M2M 6N6 (note the space between the two triplets) I want my function to validate either, i.e., allow a space between the two triplets but not enforce it. Any idea on how to modify my test? Thanks, Don -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php