• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Checking a multi select list to ensure at least ONE selected

I need the proper javascript syntax for checking a multi-select list to make sure at least ONE is selected. An example is in the code window.

When the for is submitted, I need to check to make sure at least ONE is seleted.

What is the proper syntax?
<td><select name="poolst[]" class="pt8" multiple>
																							 <option value="AL" >AL - Alabama</option>
																							 <option value="AK" >AK - Alaska</option>
																							 <option value="AZ" >AZ - Arizona</option>

																							 <option value="AR" >AR - Arkansas</option>
																							 <option value="BC" >BC - British Columbia</option>
																							 <option value="CA" >CA - California</option>
																							 <option value="CO" >CO - Colorado</option>
																							 <option value="CN" >CN - Connecticutt</option>
																							 <option value="DE" >DE - Delaware</option>

																							 <option value="DC" >DC - District of Columbia</option>
																							 <option value="ETN" >ETN - East Tennessee</option>
																							 <option value="FL" >FL - Florida</option>
																							 <option value="GA" >GA - Georgia</option>
																							 <option value="HI" >HI - Hawaii</option>
																							 <option value="ID" >ID - Idaho</option>

																							 <option value="IL" >IL - Illinois</option>
																							 <option value="IN" >IN - Indiana</option>
																							 <option value="NIN" >NIN - Indiana - North</option>
																							 <option value="IA" >IA - Iowa</option>
																							 <option value="KS" >KS - Kansas</option>
																							 <option value="KT" >KT - Kentucky</option>

																							 <option value="LA" >LA - Louisiana</option>
																							 <option value="ME" >ME - Maine</option>
																							 <option value="MD" >MD - Maryland</option>
																							 <option value="MA" >MA - Massachusetts</option>
																							 <option value="MI" >MI - Michigan</option>
																							 <option value="NMI" >NMI - Michigan - Upper Peninsula</option>

																							 <option value="MN" >MN - Minnesota</option>
																							 <option value="MS" >MS - Mississippi</option>
																							 <option value="MT" >MT - Montana</option>
																							 <option value="NV" >NV - Nevada</option>
																							 <option value="NH" >NH - New Hampshire</option>
																							 <option value="NJ" >NJ - New Jersey</option>

																							 <option value="NM" >NM - New Mexico</option>
																							 <option value="NY" >NY - New York</option>
																							 <option value="NC" >NC - North Carolina</option>
																							 <option value="ND" >ND - North Dakota</option>
																							 <option value="OH" >OH - Ohio</option>
																							 <option value="OK" >OK - Oklahoma</option>

																							 <option value="OR" >OR - Oregon</option>
																							 <option value="PA" >PA - Pennsylvania</option>
																							 <option value="RI" >RI - Rhode Island</option>
																							 <option value="SC" >SC - South Carolina</option>
																							 <option value="SD" >SD - South Dakota</option>
																							 <option value="SIN" >SIN - South Indiana</option>

																							 <option value="TN" >TN - Tennessee</option>
																							 <option value="TX" >TX - Texas</option>
																							 <option value="TEP" >TEP - Texas - El Paso</option>
																							 <option value="UT" >UT - Utah</option>
																							 <option value="VT" >VT - Vermont</option>
																							 <option value="VA" >VA - Virginia</option>

																							 <option value="WA" >WA - Washington</option>
																							 <option value="WTN" >WTN - West Tennessee</option>
																							 <option value="WV" >WV - West Virginia</option>
																							 <option value="WI" >WI - Wisconsin</option>
																							 <option value="WY" >WY - Wyoming</option>
</select>

Open in new window

0
Richard Korts
Asked:
Richard Korts
1 Solution
 
sjklein42Commented:
Change line 1 in your code to see how it works:

<td><select name="poolst" class="pt8" multiple onchange="CheckIt()">

Open in new window


<script>
function CheckIt()
{
var n,s=0;
for (n=0; n<poolst.length; n++) if ( poolst.options[n].selected ) s++;
if ( ! s ) alert("nothing selected")
else alert(s + ' items selected')
}
</script>

Open in new window

0
 
leakim971PluritechnicianCommented:
for example : http://jsfiddle.net/EYaUG/
function validate() {
    var o = document.getElementsByName("poolst[]")[0].options;
    var n = 0;
    for(var i=0;i<o.length;i++) if(o[i].selected) n++;
    if(n==0) alert("Please select a state"); // if needed uncomment that line
    return n>0; // true or false -> submit or not
}

Open in new window

0
 
Richard KortsAuthor Commented:
I have to use the version where the item is an array so I can process it properly in the next (php) program.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now