Question

How to validate the checkboxes in jquery with the minimum limit of selection. i am using the jquery plugin for form validation that validates the field by passing the field name.

jQuery('#myform').validate({

        rules: {
          checkbox_name: {
            required: true
          }


 });

now how to apply the minimum selection limit in this criteria?

Was it helpful?

Solution

The required rule is all that's needed to make at least one checkbox required.

Yours didn't work because you were missing a closing brace within .valdiate()...

$('#myform').validate({
     rules: {
         checkbox_name: {
             required: true
         }
     } // <- this bracket was missing
 });

Simple DEMO that makes one of the checkboxes from the group required...

DEMO: http://jsfiddle.net/gh96s/


If you want to required that at least X number of checkboxes from the group are selected, use the minlength rule. (you can also use the maxlength or rangelength rules)

$(document).ready(function () {

    $('#myform').validate({
        rules: {
            checkbox_name: {
               required: true,
               minlength: 2  // at least two checkboxes are required
               // maxlength: 4 // less than 5 checkboxes are required
               // rangelength: [2,4] // must select 2, 3, or 4 checkboxes
            }
        }
    });

});

Working DEMO: http://jsfiddle.net/gh96s/2/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top