Reputation: 371
I could do this:
var statevalidation = document.registration.statetextbox;
var state = statevalidation.value;
if(state=="AL" || state=="al" state=="AK" || state=="ak" || ...)
{
document.getElementById("statemsg").innerHTML=("Good to go.");
statemsg.style.color="green";
}
else
{
statemsg.innerHTML=("Invalid state.");
statemsg.style.color="red";
}
However, that leaves me with a long and unsavory if statement. I was wondering if someone knows a "cleaner" way of making sure that users put in a correct state abbreviation?
Upvotes: 0
Views: 3544
Reputation: 19953
Not exactly "cleaner", but how about something like...
var state = statevalidation.value.toUpperCase();
var states = ["AL", "AK", ...];
var found = false;
for (var i = 0; i < states.length; i++) {
if (states[i] == state) {
found = true;
break;
}
}
if (found) {
...
} else {
...
}
Upvotes: 1