Validate Canadian Postal Code Regex
Posted
by
Alex Block
on Stack Overflow
See other posts from Stack Overflow
or by Alex Block
Published on 2012-06-22T03:07:15Z
Indexed on
2012/06/22
3:16 UTC
Read the original article
Hit count: 337
I have a script written in JavaScript to Validate Canadian Postal Codes using Regex, however it does not seem to be working. Here is the script:
If statement:
if (myform.zip.value == "" || myform.zip.value == null || myform.zip.value == "Postal Code" || myform.zip.value.length < 12 ) {
alert("Please fill in field Postal Code. You should only enter 7 characters");
myform.zip.focus();
return false;
}
Function:
function okNumber(myform) {
var regex = /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/;
if (regex.test(myform.zip.value) == false) {
alert("Input Valid Postal Code");
myform.zip.focus();
return false;
}
return true;
}
© Stack Overflow or respective owner