开发者

How to use JQuery validation plugin with conditions?

I am using JQuery validation plugin to validate my form.

I have a radio button on the top. if you pick "yes", some input fields will show up. if yo开发者_StackOverflowu pick "no", different input fields will be used.

How do I change my validation codes, so It will validate those fields based on the radio button?


This is pretty simple if you're just worried about required fields. You can supply a dependency for the required rule:

$("#test").validate({
    rules: {
        Yes1: {
            required: "#yes:checked"
        },
        Yes2: {
            required: "#yes:checked"
        },
        No1: {
            required: "#no:checked"
        },
        No2: {
            required: "#no:checked"
        }
    }
});

Example: http://jsfiddle.net/andrewwhitaker/7SYcE/

Now, this gets more complicated if you're using other rules. I will expand my answer if this isn't powerful enough for you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜