Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Validation Plugin with CheckBox Group

I have a number of checkboxes listed on a page, the rendered html is as follows:

I need these grouped checkboxes to use validation so that at least one of the items is checked.

<input id="element_frm1167_8_1" name="test" class="element checkbox" type="checkbox" value="1" validate="required:true, minlength:2">
<input id="element_frm1167_8_2" name="test" class="element checkbox" type="checkbox" value="1" >
<input id="element_frm1167_8_3" name="test" class="element checkbox" type="checkbox" value="1" >

I've looked at an example from http://jquery.bassistance.de/validate/demo/radio-checkbox-select-demo.html

However, when I call form.Validate() I dont get any validation happening.

Please could someone point me in the right direction.

like image 439
BWG Avatar asked Sep 26 '12 15:09

BWG


1 Answers

I'd say that chances are you haven't configured the metadata plugin (or included it at all). That's what handles taking the validate attribute in your checkbox, and turning that into rules for the validation plugin. To get what you want in a simpler way, you can just specify the rules directly in your validate call:

$('#myForm').validate({
 rules: {
    test: {
        required: true,
        minlength:2            
    }
 } 
});

See it in action here: http://jsfiddle.net/ryleyb/EWbED/

like image 185
Ryley Avatar answered Nov 08 '22 21:11

Ryley