带有select和checkbox的jquery验证插件

时间:2011-09-08 11:33:56

标签: jquery jquery-validate

这就是我如何使用带有输入文本的jquery验证插件:

$('input[id$=txtFirstname]').rules('add', { required: true });

并且它有效,但如果想将它用于复选框并选择它不起作用......

$('input[id$=txtFirstname]').rules('add', { required: true, minlength: 2 });

1 个答案:

答案 0 :(得分:1)

尝试使用:

<form id='signupForm'>
    <input type='checkbox' name='topic' value='1'>Topic1
    <input type='checkbox' name='topic' value='2'>Topic2
    <input type='checkbox' name='topic' value='3'>Topic3
    </form>

$().ready(function() {
$("#signupForm").validate({
        rules: {
            topic: {
                required: "#newsletter:checked",
                minlength: 2
            },
},
messages: {
            topic: "you must select atleast 2 checkboxes"
}
});