Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validate only if a specific radio button is selected

Tags:

jquery

I got stuck at a particular situation

I have 2 radio buttons

<input type="radio" value="YES" id="sub" name="sub">Yes
<input type="radio" value="NO" id="sub" name="sub">No

When, Yes is selected, I need to validated 1 text-field

<input type="text" id="price" name="price" />

I am using jQuery validation plug-in

Please help me with this, Thanks.

like image 719
I-M-JM Avatar asked Jan 13 '11 05:01

I-M-JM


1 Answers

You can set up dependency validation as seen here http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression

$('#myForm').validate(rules: {
   price: { 
     required: '#sub[value="YES"]:checked'
    }
});
like image 100
Vadim Avatar answered Sep 28 '22 07:09

Vadim