Created
November 9, 2013 18:46
-
-
Save jgdoncel/7388454 to your computer and use it in GitHub Desktop.
Validar un grupo de checkboxes exigiendo que esté marcado más de uno
From http://stackoverflow.com/questions/2445010/jquery-validation-plugin-validating-checkboxes-with-different-names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.validator.addMethod('require-one', function (value) { | |
return $('.require-one:checked').size() > 0; }, 'Please check at least one box.'); | |
var checkboxes = $('.require-one'); | |
var checkbox_names = $.map(checkboxes, function(e,i) { return $(e).attr("name")}).join(" "); | |
$("#itemForm").validate({ | |
groups: { checks: checkbox_names }, | |
errorPlacement: function(error, element) { | |
if (element.attr("type") == "checkbox") | |
error.insertAfter(checkboxes.last()); | |
else | |
error.insertAfter(element); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment