Jump to content

Help validating Canadian Postal code


mufc

Recommended Posts

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 = "[email protected]";

 

/* 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.

Link to comment
https://forums.phpfreaks.com/topic/279626-help-validating-canadian-postal-code/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.