jquery 验证超过最小值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6302531/
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
jquery validation for more than min value
提问by Gowri
I am using the jquery validation pluginfor form validation. Using the min
property works fine, but I want it to validate values strictly greater thanthat min value.
我正在使用jquery 验证插件进行表单验证。使用该min
属性工作正常,但我希望它验证严格大于最小值的值。
rules: {
price: {
required: true,
min: 13,
number: true
}
}
In my code I have min: 13
, but I don't want to allow 13, only values greater than 13, e.g. 13.10, 13.20, 14. How can I do this?
在我的代码中,我有min: 13
13 个,只允许大于 13 的值,例如 13.10、13.20、14。我该怎么做?
Thanks in advance !
提前致谢 !
回答by David Tang
Create your own custom method with $.validator.addMethod
:
创建您自己的自定义方法$.validator.addMethod
:
$.validator.addMethod('minStrict', function (value, el, param) {
return value > param;
});
Then use:
然后使用:
price: {
required: true,
minStrict: 13,
number: true
}
Note:The creators of the validator plugin recommendadding Number.MIN_VALUE
to the value you supply:
注意:验证器插件的创建者建议添加Number.MIN_VALUE
您提供的值:
min: 13 + Number.MIN_VALUE
Number.MIN_VALUE
is the smallest positive (non-zero) float that JS can handle, hence the logic is that the two statements below are equivalent:
Number.MIN_VALUE
是 JS 可以处理的最小正(非零)浮点数,因此逻辑是下面的两个语句是等效的:
a > b;
a >= b + Number.MIN_VALUE;
But, this doesn't work, due to the way floating-point numbers are stored in memory. Rounding will cause b + Number.MIN_VALUE
to equal b
in most cases (b
must be very smallfor this to work).
但是,由于浮点数存储在内存中的方式,这不起作用。在大多数情况下,舍入将导致b + Number.MIN_VALUE
相等b
(b
必须非常小才能使其工作)。
回答by Daniel Miloca - Brazil
min: 13.01, this:
分钟:13.01,这个:
rules:{
price:{
required: true,
min: 13.01,
number: true
}
}
rules:{
price:{
required: true,
min: 13.01,
number: true
}
}