jQuery验证:AddMethod消息覆盖标准消息

时间:2019-04-11 08:19:13

标签: jquery validation

我有2种需要数字值(0-3之间)的输入形式:

<input type="text" id="number1" name="number1" placeholder="0" maxlength="1" class="optionsNumber" onkeypress="return (event.charCode == 8 || event.charCode == 0 || event.charCode == 13) ? null : event.charCode >= 48 && event.charCode <= 51">
<input type="text" id="number2" name="number2" placeholder="0" maxlength="1" class="optionsNumber" onkeypress="return (event.charCode == 8 || event.charCode == 0 || event.charCode == 13) ? null : event.charCode >= 48 && event.charCode <= 51">

然后我使用Jquery Validate addMethod检查number1和number2的组合值是否小于4:

<script>


$(document).ready(function() {

    $.validator.addMethod("check_months", function (value, element, param) {
        var number1 = $("#number1").val() || 0;
        var number2 = $("#number2").val() || 0;
        return this.optional(element) || parseInt(number1)+parseInt(number2) < 4;
    }, "The total number allocated to number1 and number2 cannot be more than 3.");

    $("#Form").validate({
        onkeyup: function(element) {
            this.element(element);
        },
        onfocusout: function(element) {
            this.element(element);
        },
        rules: {
            number1: {
                check_months: true,
            },
            number2: {
                check_months: true,
            },
        },
        messages: {
            number1: "Please enter the number you require number1 (max 3)",
            number2: "Please enter the number you require number2 (max 3)"
        },
        errorPlacement: function(error, element) {
            var placement = $(element).data('error');
            if (placement) {
                $(placement).append(error)
            } else {
                error.insertAfter(element);
            }
        }
    });
});
</script>

如果number1的输入字段包含2,number2的输入字段包含2,则组合值为4,因此我想显示addMethod消息(分配给number1和number2的总数不能超过3。)但是我看到的错误是验证消息(请输入您需要的数字,数字为1/2(最多3个))。

如何用addMethod消息覆盖validate消息?

更新:通过注释掉标准消息,将显示addMethod消息,但是如果用户希望两者都显示,则该怎么办:

messages: {
            //number1: "Please enter the number you require number1 (max 3)",
            //number2: "Please enter the number you require number2 (max 3)"
        },

1 个答案:

答案 0 :(得分:0)

虽然使用validate函数,但您已为每个字段指定了全局错误消息,但它会覆盖默认消息。下面的代码为我工作。请检查。

$.validator.addMethod("check_months", function (value, element, param) {
            var number1 = $("#number1").val() || 0;
            var number2 = $("#number2").val() || 0;
            return this.optional(element) || parseInt(number1)+parseInt(number2) < 4;
        }, "The total number allocated to number1 and number2 cannot be more than 3.");

        $("#Form").validate({
            onkeyup: function(element) {
                this.element(element);
            },
            onfocusout: function(element) {
                this.element(element);
            },
            rules: {
                number1: {
                    required: true,
                    check_months: true,
                },
                number2: {
                    required: true,
                    check_months: true,
                },
            },
            messages: {
                number1: { required: "Please enter the number you require number1 (max 3)", },
                number2:{ required:  "Please enter the number you require number2 (max 3)"}
            },
            errorPlacement: function(error, element) {
                var placement = $(element).data('error');
                if (placement) {
                    $(placement).append(error)
                } else {
                    error.insertAfter(element);
                }
            }
        });