// Method that validates the Basic Business Rules for UK Zip codes.
function checkPostCode(pc) { //check postcode format is valid
var test = pc;
var size = test.length
test = test.toUpperCase(); //Change to uppercase
while (test.slice(0,1) == " ") { //Strip leading spaces
test = test.substr(1,size-1);size = test.length
}
while(test.slice(size-1,size) == " ") { //Strip trailing spaces
test = test.substr(0,size-1);size = test.length
}
if (size == 0) {
return "please enter a valid postcode";
}
//document.details.pcode.value = test; //write back to form field
if (size <> 8){ //Code length rule
//return "please enter a valid postcode";
return test + " is not a valid postcode - wrong length";
}
if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
//return "please enter a valid postcode";
return test + " is not a valid postcode - cannot start with a number";
}
if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
// return "please enter a valid postcode";
return test + " is not a valid postcode - alpha character in wrong position";
}
if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
//return "please enter a valid postcode";
return test + " is not a valid postcode - number in wrong position";
}
if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
//return "please enter a valid postcode";
return test + " is not a valid postcode - number in wrong position";
}
if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
//return "please enter a valid postcode";
return test + " is not a valid postcode - space in wrong position";
}
count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
if (count1 != count2){//only one space rule
return "please enter a valid postcode";
return test + " is not a valid postcode - only one space allowed";
}
return "OK";
}
No comments:
Post a Comment