listbox validation by javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonysunny
    New Member
    • Feb 2008
    • 6

    listbox validation by javascript

    hai,

    How can i restrict number of selections in listbox using javascript.
    ie. only 3 items are allowed to select , also atleast one item should select.

    thanks in advance
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Originally posted by sonysunny
    hai,

    How can i restrict number of selections in listbox using javascript.
    ie. only 3 items are allowed to select , also atleast one item should select.

    thanks in advance
    [CODE=javascript]
    var selectedOptions = new Array(); //array containing the ids of selected options
    function doSelect(whom) {
    var temp;
    if (selectedOption s.length == 3) { //required length
    temp = selectedOptions .shift();
    document.getEle mentById(temp). selected = false;
    }
    temp = selectedOptions .push(whom.id);
    }
    [/CODE]
    On the onclick of every option, call the function doSelect(this).
    * And don't forget to set autocomplete=of f to the select input.

    Check if this works or not. I didn't check it.

    PS: Are you sure there's not any HTML attribute to restrict it?

    Comment

    Working...