JQuery check if input is not empty

Good day. Help Install the Validate plugin to update your user profile. There are two input fields: "new password"

and "repeat new password"

.

Needed if they are both empty, it does not perform any checks. And if something is entered in "Password", the password is checked for validity and equality of the second password.
I've tried something like this, but this "if" doesn't 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 searched for an answer in similar threads but couldn't find it.

+3


source to share


2 answers


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"

               }
     }

});

      

+2


source


I like the first answer but did not personally include

minlength: 6,
maxlength: 10

      



In cfmPassword. Either the first password meets the criteria or generates an error. If this does not cause an error, the second password should match.

+3


source







All Articles