dominicall
10-18-2002, 11:03 AM
Hi - hope someone can help.
I'm trying to validate a UK postcode format with the following script:
UK postcodes can be any of the following (A = alpha, N = numeric):
AN NAA, AAN NAA, ANN NAA, AANN NAA
For some reason, the following script returns true even if I enter something completely invalid such as 'dddddd'.
// check that UK post code is valid
function isValidPostcode(postcode) {
if (!(postcode.length == 6 || postcode.length == 7 || postcode.length == 8)) return false; //check not too short or long
if (!(isNaN(postcode.charAt(0)))) return false; //1st character must be a letter
if (isNaN(postcode.charAt(size-3))) return false; //first character of inward code must be numeric rule
if (!(isNaN(postcode.charAt(size-2)))) return false; //second character of inward code must be alpha rule
if (!(isNaN(postcode.charAt(size-1)))) return false; //third character of inward code must be alpha rule
if (!(postcode.charAt(size-4) == " ")) return false; //space in position length-3 rule
count1 = postcode.indexOf(" ");count2 = postcode.lastIndexOf(" "); //find first and last space - should be only 1
if (count1 != count2) return false; //only one space rule
return true;
}
Any one have any ideas????
Thanks
Dominic:confused:
I'm trying to validate a UK postcode format with the following script:
UK postcodes can be any of the following (A = alpha, N = numeric):
AN NAA, AAN NAA, ANN NAA, AANN NAA
For some reason, the following script returns true even if I enter something completely invalid such as 'dddddd'.
// check that UK post code is valid
function isValidPostcode(postcode) {
if (!(postcode.length == 6 || postcode.length == 7 || postcode.length == 8)) return false; //check not too short or long
if (!(isNaN(postcode.charAt(0)))) return false; //1st character must be a letter
if (isNaN(postcode.charAt(size-3))) return false; //first character of inward code must be numeric rule
if (!(isNaN(postcode.charAt(size-2)))) return false; //second character of inward code must be alpha rule
if (!(isNaN(postcode.charAt(size-1)))) return false; //third character of inward code must be alpha rule
if (!(postcode.charAt(size-4) == " ")) return false; //space in position length-3 rule
count1 = postcode.indexOf(" ");count2 = postcode.lastIndexOf(" "); //find first and last space - should be only 1
if (count1 != count2) return false; //only one space rule
return true;
}
Any one have any ideas????
Thanks
Dominic:confused: