开发者

jquery validation for more than min value

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.

rules: {
    pri开发者_开发百科ce: {
        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?

Thanks in advance !


Create your own custom method with $.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:

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:

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).


min: 13.01, this:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜