Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select validation question 2

Status
Not open for further replies.

JimJx

Technical User
Feb 16, 2001
202
US
Hi All,

I have a for that has a drop down for states. If a user selects a certain state, I need an alert telling him that the service is not yet available in that area.

Pretty simple, I think I can handle 1 or 2 states. But, is there an easy way to check against a list of states?

For example, out of the 50 states only 10 are available at this time. All others, the service is not available. Instead of doing 40 if statements, is there a way to streamline this?

I hope I didn't completely confuse everyone. :)

Thanks!
Jim
 
You could use something like this:
Code:
function state_abbrv(value) {
  //Case insensitive
  var re = /^(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)$/;
  return re.test(value);
}

You pass the function the state code selected and if it matches one in the function it returns true.

You would just remove all state abbreviations from the above function that are not currently supported.


At my age I still learn something new every day, but I forget two others.
 
Here's an example using an array. You would just put the states in the array that you want to be accepted, otherwise it pops up an error. In this case, I entered all the states that started with the letters A thru M:
Code:
<script type="text/javascript">
var statesThruM = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana']

function checkMe(str) {
   if (statesThruM.join("|").indexOf(str) == -1) {
      alert("The state you selected starts with a letter after M");
   }
}

</script>


<select onchange="checkMe(this.value)">
   <option value='Alabama'>Alabama</option>
   <option value='Alaska'>Alaska</option>
   <option value='Arizona'>Arizona</option>
   <option value='Arkansas'>Arkansas</option>
   <option value='California'>California</option>
   <option value='Colorado'>Colorado</option>
   <option value='Connecticut'>Connecticut</option>
   <option value='Delaware'>Delaware</option>
   <option value='Florida'>Florida</option>
   <option value='Georgia'>Georgia</option>
   <option value='Hawaii'>Hawaii</option>
   <option value='Idaho'>Idaho</option>
   <option value='Illinois'>Illinois</option>
   <option value='Indiana'>Indiana</option>
   <option value='Iowa'>Iowa</option>
   <option value='Kansas'>Kansas</option>
   <option value='Kentucky'>Kentucky</option>
   <option value='Louisiana'>Louisiana</option>
   <option value='Maine'>Maine</option>
   <option value='Maryland'>Maryland</option>
   <option value='Massachusetts'>Massachusetts</option>
   <option value='Michigan'>Michigan</option>
   <option value='Minnesota'>Minnesota</option>
   <option value='Mississippi'>Mississippi</option>
   <option value='Missouri'>Missouri</option>
   <option value='Montana'>Montana</option>
   <option value='Nebraska'>Nebraska</option>
   <option value='Nevada'>Nevada</option>
   <option value='New Hampshire'>New Hampshire</option>
   <option value='New Jersey'>New Jersey</option>
   <option value='New Mexico'>New Mexico</option>
   <option value='New York'>New York</option>
   <option value='North Carolina'>North Carolina</option>
   <option value='North Dakota'>North Dakota</option>
   <option value='Ohio'>Ohio</option>
   <option value='Oklahoma'>Oklahoma</option>
   <option value='Oregon'>Oregon</option>
   <option value='Pennsylvania'>Pennsylvania</option>
   <option value='Rhode Island'>Rhode Island</option>
   <option value='South Carolina'>South Carolina</option>
   <option value='South Dakota'>South Dakota</option>
   <option value='Tennessee'>Tennessee</option>
   <option value='Texas'>Texas</option>
   <option value='Utah'>Utah</option>
   <option value='Vermont'>Vermont</option>
   <option value='Virginia'>Virginia</option>
   <option value='Washington'>Washington</option>
   <option value='West Virginia'>West Virginia</option>
   <option value='Wisconsin'>Wisconsin</option>
   <option value='Wyoming'>Wyoming</option>
</select>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
JimJx, did our solutions help you? That was an awful lot of copying/pasting/rearranging in my above example just for the thread to get abandoned.....

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Not abandoned, but have a job in addition to doing this and had to work.

Got a chance to try out both and they both worked great.

Thanks guys.

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top