Client side javascript validation; check that a postcode matches a state

// Check that the postcode matches the state

function validate_postcode(state,postcode) {

if ((state == ‘NSW’) && ((postcode.charAt(0) == ‘1’) || (postcode.charAt(0) == ‘2’))) return(true);

if ((state == ‘ACT’) && ((postcode.charAt(0) == ‘0’) || (postcode.charAt(0) == ‘2’))) return(true);

if ((state == ‘VIC’) && ((postcode.charAt(0) == ‘3’) || (postcode.charAt(0) == ‘8’))) return(true);

if ((state == ‘QLD’) && ((postcode.charAt(0) == ‘4’) || (postcode.charAt(0) == ‘9’))) return(true);

if ((state == ‘SA’ || state == ‘SA ‘) && (postcode.charAt(0) == ‘5’)) return(true);

if ((state == ‘WA’ || state == ‘WA ‘) && (postcode.charAt(0) == ‘6’)) return(true);

if ((state == ‘TAS’) && (postcode.charAt(0) == ‘7’)) return(true);

if ((state == ‘NT’ || state == ‘NT ‘) && (postcode.charAt(0) == ‘0’) && (postcode.charAt(1) == ‘8’)) return(true);

return(false);

}

Advertisements