0

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> 
Community
  • 1
  • 1
Joppo
  • 715
  • 2
  • 12
  • 31

1 Answers1

0

In the additional-method.js file, there is a method called require_from_group that will do what you describe... make a specified number input element(s) required out of a selected group of input elements. (make sure to use the latest version (1.11.1) which fixed a bug)

$(document).ready(function () {

    $('#myform').validate({
        rules: {
            productselection0: {
                require_from_group: [1, '.require-one']
            },
            productselection1: {
                require_from_group: [1, '.require-one']
            },
            productselection2: {
                require_from_group: [1, '.require-one']
            }
        },
        groups: {
            checks: "productselection0 productselection2 productselection3"
        }
    });

});

Working DEMO: http://jsfiddle.net/Fr4y4/

Sparky
  • 98,165
  • 25
  • 199
  • 285