0

Using version 1.11.1 of the library I'm noticing that if I use something like

<input data-rule-max="8" />

it will correctly fail the validation when I enter a value of 10.

However, if I have something like

<input data-rule-max="8.00" />

and I enter 10 it doesn't invalidate the field. If I enter 9 it marks it as invalid, though.

When I debug, I can see that the max rule is receiving string values to work with: "10" is less than "8.00".

I need to be able to validate against things like 8 and 8.75. Is there a trick to get the max rule to play nice with non-integers, or another rule I should be using?

(As an aside, I will probably be updating to the latest version of the library at some point, so this question is just for what to do until I can make that change.)

0

1 Answer 1

1

Is there a trick to get the max rule to play nice with non-integers,

The max rule itself works fine... you're talking about an issue when declaring max using the data-rule-max attribute. In that case, updating the plugin is your only solution.

or another rule I should be using?

The max rule is the max rule.

If you can't update the plugin, then simply use the HTML 5 max attribute, instead of data-rule-max, to declare the max rule.

<input type="text" name="foo" max="8.00" />

DEMO (v1.11.1): http://jsfiddle.net/fguxextv/


This is an issue of how the plugin maps the attributes over to available methods. If mapping was not fixed within the plugin, you could simply declare the rule via the .validate() method. In that case, it's "max".

 $('#myform').validate({
    rules: {
        foo: {
            required: true,
            max: 8.00
        }, 
        ....

DEMO 2 (v1.11.1): http://jsfiddle.net/fguxextv/3/

Sign up to request clarification or add additional context in comments.

1 Comment

I'm glad it's that simple. Our team's coding practice is to use data attributes all the time for consistency, but I think I can talk them into making this an exception until updating the library bubbles up the priority stack. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.