0

in jquery.validate

my requirement:

the input-text will be validated only if the checkbox in the same row is be checked!

$("[name='ck']").click(function () {
        var text = $(this).parent().parent().find("[name='t']");
        if ($(this).attr("checked") == "checked") {
            text.addClass("required");
        } else {
            text.removeClass("required");
        }
    });

the code is OK! but when i change the rule to {required:true,number:true,max:5} ,it is not work!

only the input-text of the first checkbox be checked will be validated! when the second checkbox be clicked,the relevant input-text will not be validated.

so

addClass("{required:true,number:true,max:5}");

Is the way to Add/remove validation rules Dynamically is NOT legal, but why it will be work only once!

1
  • addClass() is for adding CSS classes. It doesn't work for you because the string {required:true,number:true,max:5} is not a CSS class. Commented Jun 28, 2013 at 15:55

1 Answer 1

0

You will probably want to use the depends callback to dynamically choose whether or not the rule should be required.

$(".selector").validate({
  rules: {
    contact: {
      required: true,
      email: {
        depends: function(element) {
          return $("#contactform_email:checked")
        }
      }
    }
  }
});

This example was pulled from the jquery validation page

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.