Monday, July 19, 2004

JavaScript Function to Validate U.S. State (valid state)

This function tests the passed text against the U.S. State abbreviations, returning True if the text is a U.S. State abbreviation, False otherwise.


// Make sure a state is valid.
function ValidState(sstate) {
sstates = "wa|or|ca|ak|nv|id|ut|az|hi|mt|wy" +
"co|nm|nd|sd|ne|ks|ok|tx|mn|ia|mo" +
"ar|la|wi|il|ms|mi|in|ky|tn|al|fl" +
"ga|sc|nc|oh|wv|va|pa|ny|vt|me|nh" +
"ma|ri|ct|nj|de|md|dc";

if (sstates.indexOf(sstate.toLowerCase() + "|") > -1) {
return true;
}

return false;
}