English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi, i have a little dilemma with the listbox i've set it to mulptiple so users can choose a number of items but i want to limit their selection to five only... so the problem is i have no idea how to do that. Ahh... javascript? Thanks in advance.

2007-06-10 23:10:19 · 2 answers · asked by urbound 1 in Computers & Internet Programming & Design

2 answers

You need something like this where you need to count the number of selected items and return an error message if the number exceeds 5.

var count
for (var i = 0; i < select.options.length; i++) {
if (select.options[i].selected) {
count++;
}
if (count>5){
alert('error');
return false;
} else {
return true;
}
}

2007-06-10 23:53:24 · answer #1 · answered by AnalProgrammer 7 · 0 0

in the defination of your control which you want to validate, put the below line

onchange="javascript:checkSelection()"

create the below function in your javascript area. Replace "" with the actual "id" of the control which you want to validate.

function checkSelection()
{
var f = document.forms[0];
var counter;

counter = 0;

for(var i=0;i.length;i++)
{
if(eval("f..options["+i+"].selected")==true)
{
counter = counter + 1;
}
}
if (counter > 5)
{
alert("You can select a maximum of 5 products");
return false;
}
}

2007-06-10 23:49:31 · answer #2 · answered by Dhimant 1 · 0 0

fedest.com, questions and answers