jQuery验证一个字段比另一个字段

时间:2017-12-02 08:51:12

标签: jquery validation rules

如果用户输入的值低于/高于其他字段,我想显示错误。我如何使用验证规则来执行此操作?我需要txtBetween1 超过txtLessthanequal。我还需要txtGreaterequal超过txtBetween和txtBetween2,这样用户就不会输入荒谬的值。

 $(document).ready(function () {
    $.validator.addMethod("greaterThan",
        function (value, element, param) {
            var $otherElement = $(param);
            return parseInt(value, 10) > parseInt($otherElement.val(), 10);
        });

    $("#currencyform").validate({
        messages: {
            txtFrom: "Currency value is required",
            txtTo: "Currency value is required",
            txtConversionRate: "Rate must be a number",
            txtGreaterEqual: "Rate must be more than between",
            txtBetween: "Rate must be number",
            txtBetween1: "Rate must be number",
            txtLessthanEqual: "Rate must be less than between",
        },
        rules: {
            txtConversionRate: {
                min: 1

            },
            txtGreaterEqual: {
                min: 
            },
            txtBetween: {
                min: 1
            },
            txtBetween1: {
                min: 1,
                greaterThan: "#LessthanEqual"
            },
            txtLessthanEqual: {
                min: 1
            },
        },
        focusInvalid: false,
        submitHandler: function () {
            return false;
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent().parent().after());
        },
    }) 

1 个答案:

答案 0 :(得分:0)

让用户在textbox1中输入值。假设在textbox2中你不希望他输入的值大于textbox1中的值。

您可以做的是,为textbox2创建一个jquery onKeyUp()事件,并且每次调用该函数时,该函数内部都会获取textbox1的值。如果该值大于textbox1中的值,则比较该值并做出相应的反应。