0

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/

1 Answer 1

1

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' },
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this works very well! Please, can you point me where can I find this in the online documentation?
I don't think it is documented, although there is an example of using a dependency function here khaidoan.wikidot.com/jquery-validation

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.