Javascript "MULTIPLE Selection" Select box validation?
I Have a Multiple selection select box. I need it to be validated using javascript, so that it should prompt to select atleast one value.
Below is the multiple select box I Have.
<select name="usrgrp[]" multiple="multiple" size="3">
<option value="11">abc</option>
<option value="12">def</option>
<option value="13">ghi</option>
</select>
Please help me to write the 开发者_如何学编程validation javascript for this select box.
if (referenceToForm.elements["usrgrp[]"].selectedIndex == -1) {
alert("Please select an item.");
}
the easiest way is use ! :
if (!$('select').val())
{
//fail
}
I would start with the Validation docs. If you find the need to validate more than just that field (perhaps you're building a form?), then using the Validation pluggin will help you much more than hacking together individual rules.
Give it a shot, it's well written and contains many examples.
精彩评论