View Full Version : Error check for a state?

11-24-2012, 10:46 PM
Hey guys, I have been working on a problem for school and we made an html form and are now using js functions to validate the form and send it to a server. It includes an address and i wanted to know how to error check to see if a person entered a valid state code(both capital letters).

I have a function now that I thought might work but I cant get it to work properly.


function checkForState() {
if (document.forms[0].state_registration.value != AK || AL || AR || AZ || CA || CO || CT || DC || DE || FL || GA || HI || IA || ID || IL || IN || KS || KY || LA || MA || MD || ME || MI || MN || MO || MS || MT || NB || NC || ND || NH || NJ || NM || NV || NY || OH || OK || OR || PA || RI || SC || SD || TN || TX || UT || VA || VT || WA || WI || WV || WY) {
window.alert("You did not enter a valid state code! Both letters must be capital!");


Does anyone have a way to do this that I am not thinking of? or did I just make a mistake? Any help or advice is appreciated thanks!

11-25-2012, 02:24 AM
I don't believe you syntax is correct for the if statement.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8" />
<title> Untitled </title>
<script type="text/javascript">
function checkForState() {
var StatesAbbreviations =
if (StatesAbbreviations.indexOf(document.forms[0].state_registration.value+',') == -1) {
alert("You did not enter a valid state code! Both letters must be capital!");
return false;
return true;
<form name="myForm" onsubmit="return checkForState()">
<input type="text" name="state_registration" value="" onblur="checkForState()">

11-25-2012, 02:39 AM
hey thank you so much! it worked perfectly the first time I ran it! I'm still pretty new at this so forgive my lack of coding skill!

11-25-2012, 02:59 AM
You're most welcome.
Happy to help.
Good Luck!

11-26-2012, 07:57 PM
var StatesAbbreviations =

Nice job of including "DC" for our nation's capital.

You might wish to add the postal codes for our military:

"AA;Armed Forces - Americas (non-Canada)", // military
"AE;Armed Forces - Eur, Afr, Can, Mid-East", // military
"AP;Armed Forces - Pacific", // military

and for our possessions, trust territory, and freely associated states:

"AS;American Samoa;Pago Pago", // possession
"GU;Guam;Agana", // possession
"MP;Northern Mariana Islands", // possession
"PR;Puerto Rico;San Juan", // possession
"UM;US Minor Outlying Islands;", // possession
"VI;Virgin Islands - U.S.;Charlotte Amalie", // possession
"PW;Palau", // trust territory
"MH;Marshall Islands", // freely associated state
"FM;Federated States of Micronesia", // freely associated state

and you might even notice that 90% of Canadians live within 100 miles of a U.S.A. border and our post office recognizes their Province Codes in our "State Code" spot on mail:

"BC;British Columbia;Victoria",
"NB;New Brunswick;Fredericton",
"NL;Newfoundland and Labrador;St. John's",
"NS;Nova Scotia;Halifax",
"NT;Northwest Territories;Yellowknife",
"PE;Prince Edward Island;Charlottetown",