Ivan
Ivan

Reputation: 2481

jQuery validation dependency expression with numeric field

I have this snippet:

<input type="checkbox" name="sal_cart" id="sal_cart">
<input type="text" name="sal_unit_price" id="sal_unit_price" class="required">

sal_unit_price can be numeric or string, but it's always required (can't be blank).
So far so good.
If sal_cart is checked, then sal_unit_price MUST be numeric (strings are no more allowed)
According to jQuery Validation dependency-expression, I put this:

$('#form1').validate({
    rules: {
        sal_unit_price: { number: '#sal_cart:checked' },
    },
    messages: {
        sal_unit_price: { number: 'Please fill in a number' },
    }
});

but it doesn't work: even if the checkbox is NOT checked, it still says that it requires a number
Please, can you spot what's wrong with this?

Here is a Fiddle: http://jsfiddle.net/cWD8j/

Upvotes: 0

Views: 350

Answers (1)

politus
politus

Reputation: 6086

The syntax you have used only applies to the required rule, for the other rules you need a longer syntax, passing in an object with param and depends properties

updated fiddle

$('#form1').validate({
    debug: true,
    rules: {
        sal_unit_price: { 
            number: {
                param : true,
                depends: '#sal_cart:checked'
            }
       },
    },
    messages: {
        sal_unit_price: { number: 'Please fill in a number' },
    }
});

Upvotes: 1

Related Questions