Web site development - 1166 Part V . Putting JavaScript to Work

1166 Part V . Putting JavaScript to Work Listing 43-11 (continued) USStates[ SD ] = SOUTH DAKOTA USStates[ TN ] = TENNESSEE USStates[ TX ] = TEXAS USStates[ UT ] = UTAH USStates[ VT ] = VERMONT USStates[ VA ] = VIRGINIA USStates[ WA ] = WASHINGTON USStates[ WV ] = WEST VIRGINIA USStates[ WI ] = WISCONSIN USStates[ WY ] = WYOMING The existence of this array comes in handy in determining if the user enters a valid, two-state abbreviation. Listing 43-12 shows the actual isUSState() validation function that puts this array to work. The function s first task is to assign an uppercase version of the entered value to a local variable (inputStr), which is the value being analyzed throughout the rest of the function. If the user enters something in the field (length > 0) but no entry in the USStatesarray exists for that value, the entry is not a valid state abbreviation. Time to go to work to help out the user. Listing 43-12: Validation Function for U.S. States // input value is a U.S. state abbreviation; set entered value to all uppercase // also set companion field (NAME= _expand ) to full state name function isUSState() { var inputStr = gField.value.toUpperCase() if (inputStr.length > 0 && USStates[inputStr] == null) { var msg = var firstChar = inputStr.charAt(0) if (firstChar == A ) { msg += n(Alabama = AL; Alaska = AK; Arizona = AZ; Arkansas = AR) } if (firstChar == D ) { msg += n(Delaware = DE; District of Columbia = DC) } if (firstChar == I ) { msg += n(Idaho = ID; Illinois = IL; Indiana = IN; Iowa = IA) } if (firstChar == M ) { msg += n(Maine = ME; Maryland = MD; Massachusetts = MA; + Michigan = MI; Minnesota = MN; Mississippi = MS; + Missouri = MO; Montana = MT) } if (firstChar == N ) { msg += n(Nebraska = NE; Nevada = NV) } alert( Check the spelling of the state abbreviation. + msg) gField.focus() gField.select() return false
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Leave a Reply