0

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?

1 Answer 1

2

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

txtRecipientEmail: { 
  required: function() { 
    return $("#other:checked").length && $("#txtQuantity").val() == '1'; 
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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 - yup, absolutely right, if it makes it clearer feel free to stick a > 0 in there, even though JavaScript works either way :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.