jquery验证超过最小值

时间:2021-09-04 20:12:02

I am using the jquery validation plugin for form validation. Using the min property works fine, but I want it to validate values strictly greater than that 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 !

提前致谢 !

2 个解决方案

#1


38  

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 recommend adding 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 small for this to work).

但是,由于浮点数存储在内存中的方式,这不起作用。在大多数情况下,舍入将导致b + Number.MIN_VALUE等于b(b必须非常小才能使其工作)。

#2


-2  

min: 13.01, this:

min:13.01,这个:

rules:{ price:{ required: true, min: 13.01, number: true } }

规则:{price:{required:true,min:13.01,number:true}}

#1


38  

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 recommend adding 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 small for this to work).

但是,由于浮点数存储在内存中的方式,这不起作用。在大多数情况下,舍入将导致b + Number.MIN_VALUE等于b(b必须非常小才能使其工作)。

#2


-2  

min: 13.01, this:

min:13.01,这个:

rules:{ price:{ required: true, min: 13.01, number: true } }

规则:{price:{required:true,min:13.01,number:true}}