JQuery Validation minLength plugin for flags

This is my first post here on stackoverflow and I am very impressed with the site!

My question is about the jQuery Validation plugin ... specifically about the minlength method. I have a group of flags, and I want to know if at least 2 flags have been set.

http://docs.jquery.com/Plugins/Validation/Methods/minlength#length

this link has documentation on the minlength method, which is used with a normal input field, but not with a checkbox. Can someone help me how to use if for checkboxes?

+4
source share
3 answers

There is no default method. Fortunately, you can add it.

 jQuery.validator.addMethod('has2selected',function(value, element) { return $(element).filter(':checked').length >= 2; }, 'the error message'); 

EDIT: I read the documentation again, and the minimum length should work (if not for the code that this code could do). You did not send the code, so I'm not sure if this is causing an error, but in your minLength message it should be minlength .

+2
source

I don't know about jQuery validation plugin, but I think you could do something like:

 if($('.myCheckBoxes :checked').length > 2) { alert('at least two have been checked'); } 

if your checkboxes have myCheckBoxes class

or something like:

 if($("input[type='checkbox'] :checked").length > 2) { alert('at least two have been checked'); } 
+1
source

Cannot use checkboxes. The concept of "length" to which it refers is the length of the string. You will need to make a custom validation method to get the desired behavior, or perhaps do something unusual with the dependency expression in required .

Karim79's answer is the beginning of what you would need to apply in a special validation method. (Condition, not warning.)

0
source

All Articles