繁体   English   中英

使用jQuery验证插件验证一组复选框

[英]Validating a Group of Checkboxes using the jQuery validation plugin

使用jquery验证插件时,如何验证一组复选框?

下面是我用于text的代码,但是如何对一组复选框进行操作?

<form id="newItem" action="formProcess.php" method="post">
    <input type="text" class="itemTitle" name="itemTitle" value="" autocomplete="off"><br>
    <label for="itemTitle" generated="true" class="error itemTitle" style=""></label><br>
    <input type="checkbox" name="country[]" value="US" class="destinations"><br>
    <input type="checkbox" name="country[]" value="GB" class="destinations"><br>
    <input type="checkbox" name="country[]" value="CA" class="destinations"><br>
    <input type="checkbox" name="country[]" value="SE" class="destinations"><br>
    <label for="destinations" generated="true" class="error destinations" style=""></label><br>
<input type="submit" name="Submit" value="Submit">
</form>;

$("#newItem").validate({
    rules: {
        itemTitle: {
            required: true,
            minlength: 3,
            maxlength: 60
        },
    // How is it done for a group of check boxes?
    // I'd like at least 1 destination to be selected, and a maximum of 3. This cannot be left blank.


    messages: {
        itemTitle: {
            required: "Please enter a valid name",
            minlength: "Minimum length required is 3 characters",
            maxlength: "Maximum length allowed 51 characters"
        },
    },
submitHandler: function() {
// Other code follows

尝试这个

    $("#newItem").validate({
    rules: {
        "country[]": {
            required: true,
            minlength: 1,
            maxlength: 3
        }
    },
    messages: {
        "country[]": {
            required: "Check atleast 1",
            minlength: "Min 1",
            maxlength: "Max only 3"
        }
    }

});

演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM