使用 jquery 验证“大于”函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20803900/
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-27 01:18:51  来源:igfitidea点击:

Using jquery Validation 'greaterthan' function

jqueryjquery-validate

提问by SC_Adams

I am using the jQuery Validation method "greaterThan" as described by Jon and Mike E. in Validate that end date is greater than start date with jQuery

我正在使用 Jon 和 Mike E 描述的 jQuery 验证方法“greaterThan”。在验证结束日期大于使用 jQuery 的开始日期

But when I send over: EndDate: { greaterThan: "#StartDate" } The result message is 'Must be greater than #StartDate'. I would like to do something like EndDate: { greaterThan: ["#StartDate","Start Date"] }

但是当我发送过来时:EndDate:{greaterThan:“#StartDate”} 结果消息是“必须大于#StartDate”。我想做类似 EndDate: { GreaterThan: ["#StartDate","Start Date"] } 的事情

But am unsure how to modify the "greaterThan" method. I tried changing the 'value' to 'options[0]' but that was a fail.

但我不确定如何修改“greaterThan”方法。我尝试将“值”更改为“选项[0]”,但失败了。

Any suggestions? Thanks. Sam.

有什么建议?谢谢。山姆。

回答by Sparky

There is no such rule or method called greaterThanwithin the jQuery Validate plugin. The answer you referenced is merely using the plugin's .addMethod()method to create a custom rule called greaterThan.

有没有这样的规则或方法叫做greaterThan的jQuery验证插件。您引用的答案只是使用插件的.addMethod()方法创建一个名为greaterThan.

Quote OP:

引用 OP:

"But when I send over: EndDate: { greaterThan: "#StartDate" }The result message is 'Must be greater than #StartDate'. I would like to do something like EndDate: { greaterThan: ["#StartDate","Start Date"] }."

“但是当我发送过来时:EndDate: { greaterThan: "#StartDate" }结果消息是'必须大于#StartDate'。我想做类似的事情EndDate: { greaterThan: ["#StartDate","Start Date"] }。”

Your question is written very poorly. However, if I understood it correctly,

你的问题写得很差。不过,如果我理解正确的话,

  • See the second example in the documentation, which correctly shows how to use multiple parameters within the function and error message.

  • Assuming you want to pass a second parameter in order to improve the error message, simply access this second parameter using {1}within the message.

  • Within the function, access the first parameter using params[0].

  • 请参阅文档中的第二个示例,它正确显示了如何在函数和错误消息中使用多个参数。

  • 假设您想传递第二个参数以改进错误消息,只需{1}在消息中使用访问第二个参数即可。

  • 在函数内,使用 访问第一个参数params[0]

In other words...

换句话说...

When you declare your rule with multiple parameters as in this example...

当您在本示例中使用多个参数声明规则时...

rules: {
    field: {
        customRule: [x,y]
    }
} 

Inside jQuery.validator.addMethod("customRule", function(value, element, params){...}, "message"), the parameters are accessed as follows...

在里面jQuery.validator.addMethod("customRule", function(value, element, params){...}, "message"),参数访问如下...

within the function:

在函数内:

params[0]equals x
params[1]equals y

params[0]等于x
params[1]等于y

within the message:

在消息中:

{0}equals "x"
{1}equals "y"

{0}等于“x”
{1}等于“y”

"Must be between {0} and {1}"will display as "Must be between x and y"

"Must be between {0} and {1}"将显示为“必须在 x 和 y 之间”



Assuming the function from the linked answeris correct, apply the edits as follows...

假设链接答案中的函数是正确的,请按如下方式应用编辑...

jQuery.validator.addMethod("greaterThan", function(value, element, params) {    
    if (!/Invalid|NaN/.test(new Date(value))) {
        return new Date(value) > new Date($(params[0]).val());
    }    
    return isNaN(value) && isNaN($(params[0]).val()) || (Number(value) > Number($(params[0]).val())); 
},'Must be greater than {1}.');

Declare the rule as you requested...

根据您的要求声明规则...

$("form").validate({
    rules: {
        EndDate: {
            greaterThan: ["#StartDate","Start Date"]
        }
    }
});


EDIT:

编辑

Based on comments, simply check to see if the first field is empty before doing anything else...

根据评论,在做其他任何事情之前,只需检查第一个字段是否为空...

jQuery.validator.addMethod("greaterThan", function(value, element, params) {
    if ($(params[0]).val() != '') {    
        if (!/Invalid|NaN/.test(new Date(value))) {
            return new Date(value) > new Date($(params[0]).val());
        }    
        return isNaN(value) && isNaN($(params[0]).val()) || (Number(value) > Number($(params[0]).val()));
    };
    return true; 
},'Must be greater than {1}.');

回答by u445908

Let make it simple.

让事情变得简单。

use minand maxmethd.

使用 min和方法max

amount: {
  min: 1,
  max: 10
}

refer to: https://jqueryvalidation.org/min-method/

参考:https: //jqueryvalidation.org/min-method/