0

i want to validate check box by jquery validation adding rule and message. i did this way but some where i made the mistake for which code is not working.

i want user has to select one checkbox at least before form submit.

code

<form id="myform">
    <input type="checkbox" name="test[]" class="clschk" />x
    <input type="checkbox" name="test[]"  class="clschk"/>y
    <input type="checkbox" name="test[]"  class="clschk"/>z
    <input type="submit" />
</form>
<a id="docs" href="http://docs.jquery.com/Plugins/Validation" target="_blank">Validation Documentation</a>

js code

$(document).ready(function () {
    $('#myform').validate({ // initialize the plugin
        rules: {
            '.clschk': {
                required: true,
                maxlength: 2
            }
        },
        messages: {
            '.clschk': {
                required: "You must check at least 1 box",
                maxlength: "Check no more than {0} boxes"
            }
        },
        submitHandler: function (form) { // for demo
            alert('valid form submitted'); // for demo
            return false; // for demo
        }
    });
});

2 Answers 2

2

you can add validation messages based on class by using addClassRules method and then you can override the default messages by below code

$.validator.addMethod("cRequired", $.validator.methods.required,"You must check at least 1 box");

$(document).ready(function () {
    $.validator.addClassRules({

    clschk:{
        cRequired: true,
        cmaxlength: 2
    },
    submitHandler: function (form) { // for demo
        alert('valid form submitted'); // for demo
        return false; // for demo
    }


});

$('#myform').validate();

$.validator.addMethod("cRequired", $.validator.methods.required,"You must check at least 1 box");

$.validator.addMethod("cmaxlength", $.validator.methods.maxlength,"You must check at least 1 box");

});

updated fiddle - http://jsfiddle.net/Nbcj9/398/

Sign up to request clarification or add additional context in comments.

3 Comments

what this line will do $('#myform').validate();?
$('#myform').validate() initializes the validator and the validator will start validating the form.
2

The issue is simply because the keys of the rules object must be equal to the name property of the element, not a selector for it. Try this:

$('#myform').validate({ // initialize the plugin
    rules: {
        'test[]': { // note here
            required: true,
            maxlength: 2
        }
    },
    messages: {
        'test[]': { // and here
            required: "You must check at least 1 box",
            maxlength: "Check no more than {0} boxes"
        }
    },
    submitHandler: function(form) {
        alert('valid form submitted');
        return false; // for demo
    }
});

Working example

9 Comments

Not by default using jQuery validate. You would need to re-write how their rules engine works, or find another validation library.
some time i have checkboxes whose name start with student_ after under score name is dynamic like hobbies_0_name,hobbies_1_name so on..in this case how to handle using wild card....idea plzz.
can we work with wild card for name when name is not fixed?
No, it has to be the exact name of the element.
see this fiddle jsfiddle.net/Nbcj9 they use class name...how can i use it for my code where i will set validation msg too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.