Correct, this topic has been discussed before like at validation for at least one checkbox (and related demo site http://jsfiddle.net/RGUTv/). So I copied the solution but it doesnt seem to work and I cant figure out why. The console shows the implemented functions are not triggered at all after a validate() call. I assume the error can only my html code, but what specific is the error?
Any suggestions?
Thanks in advance.
Html code:
<input type="checkbox" id="check0" name="productselection0" value="productselected0" class="require-one">
<input type="checkbox" id="check1" name="productselection1" value="productselected1" class="require-one">
<input type="checkbox" id="check2" name="productselection2" value="productselected2" class="require-one">
Javascript code (1 on 1 copy from solution at validation for at least one checkbox):
<script>
$(document).ready(function() {
$.validator.addMethod('require-one', function(value) {
console.log("addMethod triggered");
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(" ");
$("#myForm").validate({
groups: {
checks: checkbox_names
},
errorPlacement: function(error, element) {
if (element.attr("type") == "checkbox")
error.insertAfter(checkboxes.last());
else error.insertAfter(element);
}
}); //validate()
}); //function
</script>