Frank Luke
Frank Luke

Reputation: 1400

jquery validate need to set the max of a range at run-time

I have an order page where the customer needs to only be able to order from 1 up to the number in stock.

$(document).ready(function()
{
    $("#AddToCartForm").validate({
        rules: {
            quantity: { required: true, range: [1, $('.size_max').val()] }
        }
    });
});

Even though there is a hidden field with the id "size_max" and a value of 5, this returns

Please enter a value between 1 and NaN

when any number is put in the quantity field.

How can I set the upper bound of the range dynamically?

Upvotes: 0

Views: 891

Answers (3)

thekaveman
thekaveman

Reputation: 4399

If your hidden field has an ID of size_max as you indicate, then your selector should be : $('#size_max'), note the selector you are using selects elements with a class of size_max

Upvotes: 1

Mark Pieszak - Trilon.io
Mark Pieszak - Trilon.io

Reputation: 66981

I believe it might be that it is looking strictly for Integers, and that .val() returns a -typeof- string

Try doing a parseInt( $('#size_max').val() );

Upvotes: 0

Stephen Booher
Stephen Booher

Reputation: 6542

You've mentioned that you have a hidden field with an id, but you are using a class selector in your code. Try changing $('.size_max') to $('#size_max') in your code.

Upvotes: 2

Related Questions