coson
coson

Reputation: 8659

jQuery validation plugin question

I have a web form that is using this awesome plugin and I'm trying to set up a rule. In my form, I have a Quantity field. If the Quantity field is set to 1 AND a checkbox is checked, then I want to validate a field.

My form is setup like:

Quantity      TEXTBOX      id = txtQuantity
Gift?         CHECKBOX     id = #other
Recipient     TEXTBOX      id = txtRecipient

and my jQuery code is:

txtQuantity: "required",
txtRecipientEmail: { required: "#other:checked" },

I tried:

txtRecipientEmail: { required: "#other:checked", required: "$('txtQuantity').val() == '1'" }

but that's not working

Is there another way to do this?

Upvotes: 0

Views: 73

Answers (1)

Nick Craver
Nick Craver

Reputation: 630349

required can take a function as well, which is what you want here:

txtRecipientEmail: { 
  required: function() { 
    return $("#other:checked").length && $("#txtQuantity").val() == '1'; 
  }
}

Upvotes: 2

Related Questions