Limit number of selected values in listbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doogie

    Limit number of selected values in listbox

    I'm using a ASP ListBox and I have it set to allow multiple selects.
    I'd like to prevent the user from selecting more than a set number of
    values in the list box at a time (i.e. 4). What would be the best way
    to accomplish this?
  • ATR2000

    #2
    Re: Limit number of selected values in listbox

    Find answers to How to limit number of List Box options a user selects in a FORM? from the expert community at Experts Exchange


    Change your <selectto

    <select name="Category1 " id="Category1" onChange="valid ateListBox(4);"
    multiple size="5">

    then add the following script to head section

    <script>
    var origSelect = new Array();
    function validateListBox (max){
    var newSelect = new Array();
    var oSelect=documen t.getElementByI d("Category1" );
    var count=0;
    for(i=0;i<oSele ct.length;i++){
    if(oSelect.opti ons[i].selected){
    newSelect.push( i);
    ++count;
    }
    }
    for(i=0;i<newSe lect.length;i++ ){
    //alert("Hallo="+ newSelect[i]);

    }
    if(count>max){
    alert("You can only choose "+max+" or fewer categories");
    var lastItem=0;
    var match=false;
    for(i=0;i<newSe lect.length;i++ ){
    match=false;
    for(j=0;j<origS elect.length;j+ +){
    if(newSelect[i]==origSelect[j]){
    match=true;
    break;
    }
    }
    if(!match){
    oSelect.options[newSelect[i]].selected=false ;
    break;
    }
    }
    }else{
    origSelect=newS elect;
    }
    }
    </script>





    "Doogie" <dnlwhite@dtgne t.comwrote in message
    news:7dfd5627-23b2-4f34-9f5d-3a3326273871@2g 2000hsn.googleg roups.com...
    I'm using a ASP ListBox and I have it set to allow multiple selects.
    I'd like to prevent the user from selecting more than a set number of
    values in the list box at a time (i.e. 4). What would be the best way
    to accomplish this?

    Comment

    Working...