Seth
Seth

Reputation: 123

jQuery Validate Custom Function - More than Minimum Value

I was looking to do what this post describes: jquery validation for more than min value

The only difference is that the field isn't required. Even if I modify the rules as listed below, it still reads the empty form field and refuses to validate. It only fails to validate when you submit the form. Any suggestions? Thanks much in advance.

$.validator.addMethod('minStrict', function (value, el, param) {
    return value > param;
});

rules: {
    price: {
        required: false,
        minStrict: 13,
        number: true
    }
}

Upvotes: 0

Views: 1162

Answers (1)

Sparky
Sparky

Reputation: 98738

Comparing your code to the default min function from the plugin...

min: function( value, element, param ) {
    return this.optional(element) || value >= param;
}

Notice the OR this.optional(element)? This part allows you to use the rule on an optional field. Otherwise, your rule will make the field mandatory.

Until you show enough code to reproduce your problem, this works fine. (you also forgot to assign a validation message)

$.validator.addMethod('minStrict', function (value, el, param) {
    return this.optional(el) || value > param;
}, "please enter more than {0}");

DEMO: jsfiddle.net/4ogdhp1f/

Upvotes: 3

Related Questions