admin管理员组

文章数量:1334389

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.

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.

Share Improve this question asked Jan 25, 2011 at 14:03 OM The EternityOM The Eternity 16.2k44 gold badges125 silver badges187 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3
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.

本文标签: Javascript quotMULTIPLE Selectionquot Select box validationStack Overflow