T_E_M_A
T_E_M_A

Reputation: 580

jQuery validate if input not empty

Good day. Help set Validate plugin to update user profile. There are two input fields, "new password" and "repeat new password".

Need if they are both empty, it does not perform any checks. And if in the "Password" something entered, the password check for validity and equality of the second password.
I tried something like this, but this "if" does not work.

$.validator.setDefaults({ ignore: ":hidden:not(select)" });

        $('#contact-form').validate({

        rules: {         
         if(passord.lenght){
            password: {
            minlength: 6,
            maxlength: 25,
            required: true,
            loginRegex: true
          },
          password_repeat: {
            minlength: 6,
            maxlength: 25,
           required: true,
            loginRegex: true,
            equalTo: "#password"
          }}
        },
});

I was looking for the answer in similar topics, but not found.

Upvotes: 3

Views: 9146

Answers (2)

brianlmerritt
brianlmerritt

Reputation: 2842

I like the first answer, but personally would not include the

minlength: 6,
maxlength: 10

In the cfmPassword. Either the first password matches the criteria, or generates an error. If that doesn't generate an error, the second password just has to match it.

Upvotes: 3

Varun
Varun

Reputation: 95

Can you try this:

<form id="formCheckPassword">
    <input type="password" class="form-control" name="password" id="password"/>
    <input type="password" class="form-control" name="cfmPassword" id="cfmPassword" />
    <input type="submit" value="submit"/>
 </form>



 $("#formCheckPassword").validate({
           rules: {
               password: { 
                 required: true,
                    minlength: 6,
                    maxlength: 10,

               } , 

                   cfmPassword: { 
                    equalTo: "#password",
                     minlength: 6,
                     maxlength: 10
               }


           },
     messages:{
         password: { 
                 required:"the password is required"

               }
     }

});

Upvotes: 2

Related Questions