2

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?

Mehar
  • 2,158
  • 3
  • 23
  • 46
  • possible duplicate of [validation for at least one checkbox](http://stackoverflow.com/questions/11512448/validation-for-at-least-one-checkbox) – CJ Ramki Mar 24 '14 at 06:13
  • If you want help, you have to make it easy for people to help you... show the HTML markup for this form. – Sparky Mar 24 '14 at 14:57

1 Answers1

2

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/

Sparky
  • 98,165
  • 25
  • 199
  • 285