views:

531

answers:

1

We have a registration form that asks for demographic information with the additional option to not provide any information. However, to keep our numbers adding up properly, we want to require you to enter all information if you select any demographics. A rendering of the form:

  • Gender
    • ( ) Male
    • ( ) Female
  • Disabilities?
    • ( ) Yes
    • ( ) No
  • Race
    • ( ) A
    • ( ) B
    • ( ) C
  • ( ) I do not wish to provide demographic information

We want to allow selection of "I do not wish..." and all options, e.g. "Male/Yes/A", but not partial answers, e.g. "Female/C".

How can we structure the .validate() rules and options to achieve this goal? Ideally, we would have a single label show/hide if your response is not sufficient.

Additional information:

+2  A: 

I'm assuming that you are using the jQuery validation plugin. If so, what you want is a custom require rule with a dependency check that allows you to make the inputs required iff the user does not choose to opt out. You might want to have a click handler on the opt-out that clears the other fields so that you don't have to check for partial entries when the user does choose to opt out.

tvanfosson
Thank you for the advice. We added a conditional required rule on the opt out radio button to require it if each of the demographics options did not have an option selected and bound a label to it.
boflynn