Search the Community
Showing results for tags 'postal code canada'.
-
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.