473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?
Sep 29 '08 #1
1 1602
http://www.experts-exchange.com/Prog..._23384906.html

Change your <selectto

<select name="Category1" id="Category1" onChange="validateListBox(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=document.getElementById("Category1");
var count=0;
for(i=0;i<oSelect.length;i++){
if(oSelect.options[i].selected){
newSelect.push(i);
++count;
}
}
for(i=0;i<newSelect.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<newSelect.length;i++){
match=false;
for(j=0;j<origSelect.length;j++){
if(newSelect[i]==origSelect[j]){
match=true;
break;
}
}
if(!match){
oSelect.options[newSelect[i]].selected=false;
break;
}
}
}else{
origSelect=newSelect;
}
}
</script>

"Doogie" <dn******@dtgnet.comwrote in message
news:7d**********************************@2g2000hs n.googlegroups.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?

Sep 29 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Vipin Kedia | last post by:
Hi I have written a code for showing the list boxes as selected using a Listitem and the selected property of the items. Now I have 2 list boxes in my page. But it shows only the selected values...
8
by: Vipin Kedia | last post by:
Hi I have written a code for showing the list boxes as selected using a Listitem and the selected property of the items. Now I have 2 list boxes in my page. But it shows only the selected values...
2
by: John | last post by:
I have a listbox that is databound when my form loads. A user can then select and option using a drop down box. When the user selects an option the corresponding items in the listbox gets selected....
3
by: Mitch | last post by:
Is the following a correct representation of the relationship of the selected index to selected item: INDEX ITEM 0 "item 1" 1 "item 2" 2 "item 3" and so on. I keep...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.