mufc Posted June 27, 2013 Share Posted June 27, 2013 How to validate Canadian Postal Code. I am stumped here and would appreciate any help. I have tried two solutions that I found online any neither one works for me. I must be missing something. 1. " /* Canada Postal code validation */ if($name == 'postal') { $postal = $_POST['postal']; if($postal != '') { if(!preg_match('/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/', $postal)) { $result['valid'] = false; $result['reason'][$name] = 'Entered Postal Code is Invalid'; } } } " 2. " /*Canadian Postal code */ function isPostalCode($postal) { $regex = "/^([a-ceghj-npr-tv-z]){1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}$/i"; //remove spaces $postal = str_replace(' ', '', $postal); if ( preg_match( $regex , $postal ) ) { return $postal; } else { return false; } } " Here is my php file without validation included. " <?php /* Set e-mail recipient */ $myemail = "mufc@shaw.ca"; /* Check all form inputs using check_input function Put these in the order that they appear on the form*/ $firstname = check_input($_POST['firstname'], "Enter your first name"); $lastname = check_input($_POST['lastname'], "Enter your last name"); $ammount = check_input($_POST['ammount'], "Write ammount requested"); $first = check_input($_POST['first'], "Write first mortgage balance"); $estimated = check_input($_POST['estimated'], "Write estimated house value"); $assessed = check_input($_POST['assessed'], "Write assessed house value"); $present = check_input($_POST['present'], "Write Present Address"); $city = check_input($_POST['city'], "Write City/Town"); $email = check_input($_POST['email'],"Write Email Address"); $postal = check_input($_POST['postal'], "Write Postal Code"); $likeit = check_input($_POST['likeit']); $status = check_input($_POST['status'], "Please Choose Marital Status"); $comments = check_input($_POST['comments'], "Write your comments"); /* Canada Postal code validation */ if($name == 'postal') { $postal = $_POST['postal']; if($postal != '') { if(!preg_match('/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/', $postal)) { $result['valid'] = false; $result['reason'][$name] = 'Entered Postal Code is Invalid'; } } } /* Let's prepare the message for the e-mail */ $message = "Hello! Your contact form has been submitted by: First Name: $firstname Last Name $lastname E-mail: $email Ammount Requested: $ammount First Mortgage Balance: $first Estimated House Value: $estimated Assessed Value: $assessed Present Address: $present City/Town: $city Postal Code: $postal Marital Status: $status Like the website? $likeit Comments: $comments End of message "; /* Send the message using mail() function */ mail($myemail, $subject, $message); /* Redirect visitor to the thank you page */ header('Location: thanks.htm'); exit(); /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?> <html> <body> <b>Please correct the following error:</b><br /> <?php echo $myError; ?> </body> </html> <?php exit(); } ?> " Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/279626-help-validating-canadian-postal-code/ Share on other sites More sharing options...
boompa Posted June 27, 2013 Share Posted June 27, 2013 First, you should be posting code in code tags. Second, in what way isn't it working? What is your input and output? Quote Link to comment https://forums.phpfreaks.com/topic/279626-help-validating-canadian-postal-code/#findComment-1438174 Share on other sites More sharing options...
phdphd Posted June 27, 2013 Share Posted June 27, 2013 $pattern_preg_match="#^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$#";if (preg_match($pattern_preg_match, $_POST['postal'])).... Does this help ? Quote Link to comment https://forums.phpfreaks.com/topic/279626-help-validating-canadian-postal-code/#findComment-1438195 Share on other sites More sharing options...
mufc Posted June 28, 2013 Author Share Posted June 28, 2013 I can put any postal code in and it accepts it even one I know is wrong. phdphd I tried your advice and got back SCREAM: Error suppression ignored for ( ! ) Parse error: syntax error, unexpected '.' in C:\wamp\www\formext\contact.php on line 23 removed .... at the end and it went through but it is wrong because it accepts a number as first digit of postal code I know I should have put my code in tags but it has been a long time since I posted in a web design forum and I could not remember how to use code tags. Sorry Quote Link to comment https://forums.phpfreaks.com/topic/279626-help-validating-canadian-postal-code/#findComment-1438318 Share on other sites More sharing options...
boompa Posted June 28, 2013 Share Posted June 28, 2013 Let me guess...you copied and pasted his ellipsis as well Quote Link to comment https://forums.phpfreaks.com/topic/279626-help-validating-canadian-postal-code/#findComment-1438353 Share on other sites More sharing options...
mufc Posted June 28, 2013 Author Share Posted June 28, 2013 yes Quote Link to comment https://forums.phpfreaks.com/topic/279626-help-validating-canadian-postal-code/#findComment-1438363 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.