Validate Canadian Postal Code Regex
- by Alex Block
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;
}