OM The Eternity
OM The Eternity

Reputation: 16244

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.

Upvotes: 0

Views: 13374

Answers (3)

Yoesoff
Yoesoff

Reputation: 388

the easiest way is use ! :

   if (!$('select').val())
    {
         //fail       
    }

Upvotes: 1

McStretch
McStretch

Reputation: 20675

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.

Upvotes: 0

RoToRa
RoToRa

Reputation: 38441

if (referenceToForm.elements["usrgrp[]"].selectedIndex == -1) {
  alert("Please select an item.");
}

Upvotes: 3

Related Questions