views:

222

answers:

2
+1  Q: 

jQuery validation

Hi. I have a form

    <form action="/Account/Create" id="account" method="post">

    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="Password">Password:</label>
            <input id="Password" name="Password" type="text" value="" />

        </p>

        <p>
            <label for="OpenID">OpenID:</label>
            <input id="OpenID" name="OpenID" type="text" value="" />

        </p>

        <p>
            <label for="HaveID">Have OpenID:</label>

            <input checked="checked" id="ChBx" name="ChBx" type="checkbox"/>

        </p>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

</form>

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?

A: 

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

Haim Evgi