Checking a multi select list to ensure at least ONE selected

Posted on 2011-10-22
Last Modified: 2012-05-12
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>

Open in new window

Question by:Richard Korts
    LVL 16

    Expert Comment

    Change line 1 in your code to see how it works:

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

    Open in new window

    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')

    Open in new window

    LVL 81

    Accepted Solution

    for example :
    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


    Author Closing Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    A common challenge, or question, when working with two calendar dates is: What is the difference between the specified days? As is frequently the case, the answer is: It depends... What kind of difference do you want? Given two date obje…
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now