Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Error check for a state?

    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.

    :code:

    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!");
    }
    document.forms[0].state_registration.focus();
    }

    :code:

    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!

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts

    Lightbulb

    I don't believe you syntax is correct for the if statement.
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <meta charset="utf-8" />
    <title> Untitled </title>
    <script type="text/javascript">
    function checkForState() {
      var StatesAbbreviations =
          '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,';
      if (StatesAbbreviations.indexOf(document.forms[0].state_registration.value+',') == -1) {
        alert("You did not enter a valid state code! Both letters must be capital!");
        document.myForm.state_registration.focus();
        return false;
      }
      return true;
    }
    </script>
    </head>
    <body>
    <form name="myForm" onsubmit="return checkForState()">
     <input type="text" name="state_registration" value="" onblur="checkForState()">
    </form>
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts
    You're most welcome.
    Happy to help.
    Good Luck!

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    632
    Thanks
    1
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by jmrker View Post
    Code:
      var StatesAbbreviations =
          '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,';
    Nice job of including "DC" for our nation's capital.

    You might wish to add the postal codes for our military:
    Code:
    "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:
    Code:
    "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:
    Code:
    "AB;Alberta;Edmonton",
    "BC;British Columbia;Victoria",
    "MB;Manitoba;Winnipeg",
    "NB;New Brunswick;Fredericton",
    "NL;Newfoundland and Labrador;St. John's",
    "NS;Nova Scotia;Halifax",
    "NT;Northwest Territories;Yellowknife",
    "NU;Nunavut;Iqaluit",
    "ON;Ontario;Toronto",
    "PE;Prince Edward Island;Charlottetown",
    "QC;Quebec;Quebec",
    "SK;Saskatchewan;Regina",
    "YT;Yukon;Whitehorse",


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •