jQuery 验证小于

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1260984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:01:42  来源:igfitidea点击:

jQuery validate less than

jqueryjquery-validate

提问by John Oxley

I am trying to write a Less thanvalidator for jQuery.

我正在尝试Less than为 jQuery编写一个验证器。

I want to compare one text box against another, so if I have:

我想将一个文本框与另一个文本框进行比较,所以如果我有:

<input type="text" id="value1" /> <input type="text" id="value2" />

I want my validatorto look like

我想要我validator的样子

$('#myForm').validate({rules: { value1: { lessThan: "#value2" } } });

I have tried this but I can't get it to work:

我试过这个,但我无法让它工作:

$.validator.addMethod('lessThan', function(value, element, param) {
    var i = parseInt(value);
    var j = parseInt($(param).val());
    return i >= j;
}, "Less Than");

Another question is where should I put that code? In $(document).readyor just in a <script>tag?

另一个问题是我应该把代码放在哪里?在标签中$(document).ready还是仅仅在<script>标签中?

回答by John Oxley

I'm an idiot. I had made some typo's in my actual code and I'd messed up the this.optional(element) that I see in a lot of validator methods. Here is the working function:

我是个白痴。我在实际代码中犯了一些错字,并且我在很多验证器方法中看到的 this.optional(element) 都搞砸了。这是工作功能:

$.validator.addMethod('lessThanEqual', function(value, element, param) {
    if (this.optional(element)) return true;
    var i = parseInt(value);
    var j = parseInt($(param).val());
    return i <= j;
}, "The value {0} must be less than {1}");

Here is the condensed version

这是精简版

$.validator.addMethod('lessThanEqual', function(value, element, param) {
    return this.optional(element) || parseInt(value) <= parseInt($(param).val());
}, "The value {0} must be less than {1}");

Now I need to figure out how to rerun validation for field 1 when I change field2.

现在我需要弄清楚如何在更改 field2 时重新运行字段 1 的验证。

回答by RaYell

I think you can do that without actually writing your own validator method.

我认为您可以在不实际编写自己的验证器方法的情况下做到这一点。

$('#myForm').validate({
    rules: {
         value1: {
             maxlength: $('#value2').val().length
         }
    }
});

$('#value2').change(function() {
    $('#value1').rules('remove', 'maxlength').rules('add', {
         maxlength: $('#value2').val().length
    });
});

or even better without code duplication

甚至没有代码重复甚至更好

function getRule() {
    return {
        maxlength: $('#value2').val().length
    };
}

$('#myForm').validate({
    rules: {
         value1: getRule()
    }
});

$('#value2').change(function() {
    $('#value1').rules('remove', 'maxlength').rules('add', getRule());
});

回答by FerrousOxide

You can insert your validation method within any document ready block, like the one shown below.

您可以在任何文档就绪块中插入验证方法,如下所示。

$().ready(function() {

    $.validator.addMethod("lessThan",
        function(value, element, param) {
            var i = parseFloat(value);
            var j = parseFloat(param);
            return (i < j) ? true : false;
        }
    );

});

I've tried to keep this simple so that you can modify it. If the method is called "lessThan" then it should do just that. If your method actually performs "less than or Equal To" consider a more appropriate name.

我试图保持这个简单,以便您可以修改它。如果该方法被称为“lessThan”,那么它应该这样做。如果您的方法实际上执行“小于或等于”,请考虑更合适的名称。

Please note that I am also using parseFloat, allowing the method more flexibility than parseInt.

请注意,我也在使用 parseFloat,这使得该方法比 parseInt 更灵活。

From within your validator, you were using it correctly; so for a validation of say, less than 10:

在您的验证器中,您正确地使用了它;所以对于验证来说,小于 10:

$('#myForm').validate({ rules: { value1: { lessThan: "10"}} });

Good luck!

祝你好运!

回答by ????

I think its better to use HTML maxattribute.

我认为最好使用 HTMLmax属性。

<input type="number" placeholder="Marks" required name="marks" max="100">

validation will be Please enter a value less than or equal to 100. Where as you can also change max value to 99 for only less than 100 value.

验证将是请输入一个小于或等于 100 的值。因为您也可以将最大值更改为 99,仅适用于小于 100 的值。

回答by Moaaid Jamal

Consider (A) is the name of the input that you want the value of it to be less than (#B) input. this code worked with me after making the type of the two inputs is: type="number"

考虑 (A) 是您希望它的值小于 (#B) 输入的输入名称。在使两个输入的类型为:type="number"

$("#form").validate({
    rules: {
        A: {
            lessThan: "#B"
        }
    }
});