jQuery验证不允许输入字段为零

时间:2018-11-20 18:47:14

标签: jquery validation

如何禁止用户在输入字段中输入零?我正在使用Jquery验证插件和数字验证方法。我希望用户能够输入正数和负数并禁止零。

我尝试使用GreaterThan和lessThan方法,但是我似乎无法使其正常工作。

     $("#CPO_AddItemForm").validate({
        errorElement: "em",
        errorPlacement: function(error, element) {
            error.appendTo(element.parent("div").next("div"));
        },
        rules: {
            Stockno: {required: true, nowhitespace: true},
            Descrip: "required",
            Item_Qty: {required: true, number: true, greaterThan: 0},
            Price: {required: true, number: true},
            Unit: "required",
            Vac_Occ: "required",
        },
        messages: {
            Stockno: {required: "Required"},
            Descrip: {required: "Required"},
            Item_Qty: {required: "Required"},
            Price: {required: "Required"},
            Unit: {required: "Required"},
            Vac_Occ: {required: "Required"},
        },

    });

1 个答案:

答案 0 :(得分:0)

$.validator.addMethod("notOnlyZero", function (value, element, param) {
    return this.optional(element) || parseInt(value) > 0;
});

$('form').validate({
    rules: {
        yourFieldName: {
            notOnlyZero: '0'
        }
    },
    messages: {
        yourFieldName: {
            notOnlyZero: 'Message to show'
        }
    }
});