views:

24731

answers:

5

How to perform validation for a radio button group (one radio button should be selected) using jQuery validation plugin?

+11  A: 

Example on the validation demo pages

http://jquery.bassistance.de/validate/demo/radio-checkbox-select-demo.html

redsquare
+4  A: 

use the following rule for validating radio button group selection

myRadioGroupName : {required :true}

myRadioGroupName is the value you have given to name attribute

Mahes
Note that if you want to control the position of the label, you can provide your own error label where you want it with the text you would like: <label for="myRadioGroupName" class="error" style="display:none;">Please choose one.</label>
Tom
A: 

The given link does not work anymore. Is the bassistance.de site taken off now? Has the plugin been moved over to a new developer site? Or is it being discontinued?

eFrontier
it's online. the plugin is definitely not being discontinued: it's one of the most popular!
pixeline
+1  A: 

With newer releases of jquery (1.3+ I think), all you have to do is set one of the members of the radio set to be required and jquery will take care of the rest:

<input type="radio" name="myoptions" value="blue" class="required"> Blue<br />
<input type="radio" name="myoptions" value="red"> Red<br />
<input type="radio" name="myoptions" value="green"> Green

The above would require at least 1 of the 3 radio options w/ the name of "my options" to be selected before proceeding.

The label suggestion by Mahes, btw, works wonderfully!

Brandon Rome
A: 
You can also use this:
<fieldset>
<input type="checkbox" name="myoptions[]" value="blue"> Blue<br />
<input type="checkbox" name="myoptions[]" value="red"> Red<br />
<input type="checkbox" name="myoptions[]" value="green"> Green<br />
<label for="myoptions[]" class="error" style="display:none;">Please choose one.</label>
</fieldset>

and simply add this rule
rules: {
'myoptions[]':{ required:true }
}
Haider Abbas