views:

26

answers:

1

Good Day,

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?

TIA,

coson

+1  A: 

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

txtRecipientEmail: { 
  required: function() { 
    return $("#other:checked").length && $("#txtQuantity").val() == '1'; 
  }
}
Nick Craver
Since I'm going to try this in a little while, if the checkbox is checked, I assume that length will return 1 else 0, right?
coson
@coson - yup, absolutely right, if it makes it clearer feel free to stick a `> 0` in there, even though JavaScript works either way :)
Nick Craver
right on! thanks
coson