how to match password and confirm password using jquery?

Check password or confirm password is match or not.

File name : index.php

<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript">
function checkPasswordMatch() {
var password = $("#password").val();
var confirmPassword = $("#repassword").val();

if (password != confirmPassword)
$("#divCheckPasswordMatch").html("Passwords do not match!");
else
$("#divCheckPasswordMatch").html("Passwords match.");
}

$(document).ready(function () {
$("#repassword").keyup(checkPasswordMatch);
});

</script>

</head>
<body>
<input class="form-control" placeholder="password" type="password" name="password" id="password">

<input class="form-control" placeholder="Repeat password" type="password" name="repassword" id="repassword" onChange="checkPasswordMatch();">
<div class="registrationFormAlert" id="divCheckPasswordMatch"> </div>

<button type="submit" class="btn btn-primary btn-block"> Create Account </button>

</body>
</html>

Example :-

File name : index.php

<form method="post" name="form1" id="form-password" action="">
<fieldset>
<label>Password:</label>
<input type="password" name="password" id="password" value="" size="32" />
<label>Re-Enter Password:</label>
<input type="password" name="password-check" id="password-check" value="" size="32" />
<input type="submit" value="Submit" id="submit">
</fieldset>
</form>


jQuery(function(){
$("#submit").click(function(){
$(".error").hide();
var hasError = false;
var passwordVal = $("#password").val();
var checkVal = $("#password-check").val();
if (passwordVal == '') {
$("#password").after('<span class="error">Please enter a password.</span>');
hasError = true;
} else if (checkVal == '') {
$("#password-check").after('<span class="error">Please re-enter your password.</span>');
hasError = true;
} else if (passwordVal != checkVal ) {
$("#password-check").after('<span class="error">Passwords do not match.</span>');
hasError = true;
}
if(hasError == true) {return false;}
});
});





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here