besmart besmart -4 years ago 78
jQuery Question

Cannot use confirm password validation with jquery

I have a simple confirm password jquery script which simply does not work...
the form gets validated with every password of every length.

This is the form

<form action="${updatePassword}" method="post" class="form-signin" id="form-signin">

<div class="form-group">
<input type="password" id="password" name="password" placeholder="Enter new Password..." class="form-password form-control">
</div>
<div class="form-group">
<input type="password" name="confirmpassword" placeholder="Confirm Password..." class="form-password form-control" id="confirmpassword">
</div>
<button type="submit" class="btn" id="submit" name="submit> Reset password</button>
</form>


and this is the script:

$(function () {
$("#submit").click(function () //input button click
{
$.validator.setDefaults({
debug: true,
success: "valid"
});

$("#form-signin").validate({
rules: {
password: {
required: true,
minlength: 5

},

confirmpassword: {
equalTo: "#password"
}

},
messages: {
password: {
required: "Inserire una password",
minlength: "La password deve contenere almeno 5 caratteri"
},
confirmpassword: {
equalTo: "Le due password devono coincidere"
}
}

});
});
});


It looks quite linear...

Answer Source

When button type is submit then no need to check for click event. The form will automatically validated if you have used jquery validation plugin.

And modify your code like belew:

                 $("#form-signin").validate({
               rules: {
                   password: { 
                     required: true,
                     minlength: 5

                   } , 

                       confirmpassword: { 
                        equalTo: "#password"
                   }

               },
               messages: {
                   password: {
                       required: "Inserire una password",
                       minlength: "La password deve contenere almeno 5 caratteri"
                   },
                   confirmpassword: {
                       equalTo: "Le due password devono coincidere"
                   }
               }

           });

Here is Working Code

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download