Skip to content

Require Field if certain radio buttons are checked in jQuery Validation Plugin

2011 November 18

I have a form with 3 radio buttons:

<input id="Desktop" name="Usage" type="radio" value="Desktop" /> Desktop
<input id="Laptop" name="Usage" type="radio" value="Laptop" /> Laptop
<input id="iPad" name="Usage" type="radio" value="iPad" /> iPad

If one of the first two radio buttons are checked then I dynamically ask the user if they are using a PC or a MAC, which is a set of radio’s called MacOrPC. I want the PC/MAC question to be required only if the Desktop or Laptop radio buttons are checked. To do this, I think I figured out the syntax in the jQuery Validation Plugin. Here’s the rule syntax I used that seems to work using the ampersand:

rules: {
MacOrPC: {required: "#Desktop:checked" & "#Laptop:checked"}
}

One Response leave one →
  1. Dave permalink
    October 26, 2012

    Thank you for this. I have been trying to find a way to do this for a few hours now and your way is so simple. Worked like a charm.

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS