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!

How to limit multiple drop down select to 8 selections? 1

Status
Not open for further replies.

olchik

Programmer
Jan 6, 2006
93
US
Hello,

does anybody know how to limit multiple drop down selection box to 8 (user can only select up to 8 options)

Thank you very much!
 
check the number of indexs of the select list array

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Hi 1DMF, thank you for your response. Theoretically I know that I need to check the number of indexes:) Practically I don't know how to do that. Could you please give me an example?

Thank you!
 
If the element is like this.
[tt]
<select name="x" multiple="multiple" size="11" onblur="countit(this)">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
<option value="f">f</option>
<option value="g">g</option>
<option value="h">h</option>
<option value="i">i</option>
<option value="j">j</option>
<option value="k">k</option>
</select>
[/tt]
The handler countit() shows you how to obtain the counts of options selected.
[tt]
function countit(obj) {
var n=0;
for (var i=0;i<obj.options.length;i++) {
if (obj.options.selected) n++;
}
alert("# of options you've selected: " + n};
if (n>=8) {
alert("warning: # of options selected shouldn't be more than 8.")
}
}
[/tt]
You can do things based on the count in the onsubmit validation or elsewhere. The algorithm is still the same. The rest is your call.
 
Hi tsuji,

thank you very much! It works!

Thank you again!
Olchik
 
have a star tsuji

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top