0

Possible Duplicate:
JQuery validation-select required if checkbox checked

I'm validating a form with jQuery Validation. I'm trying to come up with a rule that will validate fields as required if a field in a particular set of fields is filled:

<form id="donate-form">
    <input type="text" name="full_name" required="required" /><br />
    <input type="text" name="address1" /><br />
    <input type="text" name="address2" /><br />
    <input type="text" name="city" /><br />
    <select name="state">
        <option value="">- State -</option>
        <option value="va">Virginia</option>
        <option value="md">Maryland</option>
    </select><br />
    <input type="text" name="zip" />
    <input type="submit" name="submit" />
</form>

All address fields are optional unless someone fills out address1, city, state, or zip, then they should all be required.

Community
  • 1
  • 1
Ralph
  • 346
  • 2
  • 7

1 Answers1

3

Example

Using depends, fill in your requirements.

var depends = function() {
    var vals = '';
    $('form > *').not(':eq(0)').each(function () {
        vals += $(this).val();
    });

    return vals.length > 0;
};

You should change the selectors to something more specific to fit your needs. ie not $('form')

Joe
  • 80,724
  • 18
  • 127
  • 145