Welcome to WebmasterWorld Guest from

Forum Moderators: incrediBILL

Message Too Old, No Replies

restrict select multiple

9:36 am on Aug 9, 2011 (gmt 0)

Junior Member

joined:May 3, 2011
votes: 0


I was wondering if there is a way to restrict a <select multiple> listbox to maximum 2 options selected? Couldn't find an answer crawling the net..
11:41 am on Aug 9, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0

AFAIK there is no way to do this in HTML alone. You could perhaps use JavaScript to limit the number of selections. (Ultimately you will need to validate this in your server-side script.)
3:43 pm on Aug 9, 2011 (gmt 0)

Junior Member

joined:May 3, 2011
votes: 0

thanks penders
7:47 am on Aug 10, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2010
posts: 114
votes: 0

Onclick you'll have to save the selections to an array, after the change of the selection, you count the array and if there are to many values, you reset the array to what it was before.....

<select name="test" size="7" multiple="multiple" onclick="saveSelected(this)" onchange="checkSelectedValues(this, 3)">
var selected = [];
function saveSelected(obj) {
// Before the new selectection, we save all the values
selected = [];
for(i=0;i<obj.options.length;i++) {
if(obj.options[i].selected) {
function checkSelectedValues(obj, max) {
// After selection we check the amount of selections
// And reset the selection if there are to many
if(selected.length >= max) {
alert("To many selected values");
// Uncheck all
obj.options[i].selected = false
// Check the last known batch of options
obj.options[ selected[i] ].selected = true;

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members