jQuery Validation: Changing Rules Dynamically

后端 未结 10 910
栀梦
栀梦 2020-12-07 13:07

I have a single form that, depending on which radio button is clicked (Login or Signup), displays either:

  • email address
  • password

or:

10条回答
  •  -上瘾入骨i
    2020-12-07 13:54

    Or you can use the depends property.

    http://jqueryvalidation.org/category/plugin/ search for depends

    Example: Specifies a contact element as required and as email address, the latter depending on a checkbox being checked for contact via email.

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

提交回复
热议问题