Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: {
    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?

Thanks in advance !

like image 210
Gowri Avatar asked Jun 10 '11 05:06

Gowri


2 Answers

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

like image 122
David Tang Avatar answered Nov 12 '22 10:11

David Tang


min: 13.01, this:

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

like image 1
Daniel Miloca - Brazil Avatar answered Nov 12 '22 12:11

Daniel Miloca - Brazil