I am trying to do the validation with jquery validator. For the first time every thing works fine.
But from second time, without changing any thing in the password field, if i go to the confirm password section, password ideally will show error but at the same time confirm password is also showing error, "confirm password not matches to password."
Confirm password should show error only after entering the wrong password which not matches with the password.
Is there any solution for this issue or Do i need to write my own custom method extending the validator method.
Here it is what I tried:
$(document).ready(function(){
$("#cp").validate(validateChangePswrdForm("changePasswordForm"));
});
var validateChangePswrdForm = function() {
var that = this;
return {
rules: {
password: {
required: true,
minlength: 8,
},
confirmpassword: {
equalTo: "#password"
}
},
messages: {
password: {
required: "password is required",
minlength: 5
},
confirmpassword: {
required: "confirm password is required",
minlength: 5
}
},
submitHandler: function(fId, evt) {
var serForm = $(fId).serialize();
changepassword(fId, evt)
}
}
};
var changepassword = function(){
alert('Validated');
};
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min.js"></script>
<script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div id="cp">
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-8 col-sm-7">
<form method="post" role="form" autocomplete="off" name="changePasswordForm" id="changePasswordForm">
<div class="alert alert-danger changepassword-error hidden">
</div>
<div class="form-group">
<label for="password">password</label>
<input type="password" class="form-control" name="password" id="password" placeholder="password">
</div>
<div class="form-group reg-form col-sm-7 padLeft">
<label for="confirmpassword">Confirm Password</label>
<input type="password" class="form-control noradius" name="confirmpassword" id="confirmpassword" placeholder="Confirm Password">
</div>
<div class="btn-toolbar">
<button type="submit" class="btn">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>