开发者

jQuery validate only if a specific radio button is selected

开发者 https://www.devze.com 2023-02-04 16:33 出处:网络
I got stuck at a particular situation I have 2 radio buttons <input type=\"radio\" value=\"YES\" id=\"sub\" name=\"sub\">Yes

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="s开发者_JAVA技巧ub" 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.


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'
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消