I have added a Types multicheckboxes field and want to add two custom validations to this field:
–The first one is to make the field required
–The second one is to force the client to select maximum two checkboxes
jQuery(document).ready(function() { \This adds the validation method to our API so that CRED can recognize it jQuery.validator.addMethod("maxselections", function(value, element, param) { var element_name = jQuery(element).attr('name'); return (jQuery('input[name="' + element_name + '"]:checked').length !=0 &&jQuery('input[name="' + element_name + '"]:checked').length <= param[1]); }); \This adds the validations methods as attributes on the element itself. jQuery('input[name="wpcf-postscheckboxesfield[]"]').attr('data-wpt-validate', '{}') .data('wpt-validate', { maxselections: { args: { 1: 2 }, message: 'You have to choose at least 1, maximum two items' } }); });