Markus
Markus

Reputation: 3627

jQuery validation

Fields

Password:

OpenID:

Have OpenID:

I want to validate this form using jQuery. I want that in case the checkbox is checked, the password should be validated, in case it is not check - the openid should be validated. I tried this:

$().ready(
   function()
   { 
       $("#account").validate
       (
        {
            errorLabelContainer: $("ul", $('div.error-container')),
            wrapper: 'li',

            rules:
            {
                Password:
                {
                    required: true,
                    minlength: 6,
                    maxlength: 20
                },

                OpenID:
                {
                   required: true,
                   minlength: 6
                }
            },

            messages:
            {
                Password:
                {
                    required: "Enter password.",
                    minlength: "Min length is 6.",
                    maxlength: "Max length is 20."
                },

                OpenID:
                {
                   required: "Enter open id.",
                   minlength: "Min length is 6"
                }
             }
          }
       )
  }

);

This code doesn't take into account whether the checkbox is checked or not. I know about
the function http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-callback that can applied to the requiered rule, but it works with required rule. How can I manage to validate the form?

Upvotes: 1

Views: 2332

Answers (1)

Haim Evgi
Haim Evgi

Reputation: 125604

you can add a method , your function that check it jQuery.validator.addMethod

Upvotes: 1

Related Questions