regex – Matching state to postcode Javascript


Ok so I’m a little confused I’m meant to match up the Australian state with the correct Australian postcode

۱٫create a variable called state and assign it the value of whatever has been chosen in your select input

۲٫switch statement to determine what RegEx to use based on the state choice.

۳٫The code to test the regex above against the postcode entered goes here.
Eg. if(!postcodeRegEx.test(document.getElementById(“postcode”).value))

function validPostCode() {
var state = (document.getElementById("state").value);

 switch (state)
{
    case "SA":
    var postcodeRegEx=/^5[0-9]{3}$/;
    break;
case "NSW":
    var postcodeRegEx=/^2[0-9]{3}$/;
    break;
case "VIC":
    var postcodeRegEx=/^3[0-9]{3}$/;
    break;
case "QLD":
    var postcodeRegEx=/^4[0-9]{3}$/;
    break;
case "TAS":
    var postcodeRegEx=/^7[0-9]{3}$/;
    break;
case "WA":
    var postcodeRegEx=/^6[0-9]{3}$/;
    break;
case "ACT":
    var postcodeRegEx=/^2[0-9]{3}$/;
    break;
case "NT":
    var postcodeRegEx=/^8[0-9]{3}$/;
    break;
}
if(!postcodeRegEx.test(document.getElementById("postcode").value)) 
{
    //failure
return false;
}
else
{
//success

return true;

}



لینک منبع

پاسخ دهید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *