vendredi 1 avril 2016

Warning messages for two groups of checkboxes

Hi guys I have jQuery code to validate if checkboxes are checked, and if they are not warning messages are displayed. All works fine. Here is a code:

$(document).ready(function() {
    var checkboxes = $('.require-one');
    var checkbox_names = $.map(checkboxes, function(e, i) {
        return $(e).attr("name")
    }).join(" ");

    $("#itemForm").validate({
        groups: {
            checks: checkbox_names
        },
        rules: {
            resp01: 'required',
        },
        messages: {
            resp01: {
                required: 'You must agree before submitting!'
            },
        },
        errorPlacement: function(error, element) {
            $('#form_error1').append(error);
        },
        errorPlacement: function(error, element) {
            $('#form_error').append(error);
        },
        submitHandler: function(form) {
            alert('Form Submited');
            return false;
        }

    });
});

$.validator.addMethod('require-one', function(value) {
    if ($('#resp01').is(':checked')) {
        return $('.require-one:checked').size() > 0;
    } else {
        return true;
    }
}, 'Please select one of the options.');

There are two messages for two different groups of checkboxes. At the moment both messages are displayed under last checkbox. What I want is to display messages under appropriate section. Here is JSFiddle. Any help appreciated.




Aucun commentaire:

Enregistrer un commentaire