用于在键入时验证密码的Javascript

时间:2016-06-29 21:21:48

标签: javascript

我正在尝试在键入时验证密码匹配但是,它不起作用可以有人请告诉我错误

这是代码

<!DOCTYPE html>
<html>
<head>
<title>password change </title>
<script>
$(document).ready(function () {
   $("#txtConfirmPassword").keyup(checkPasswordMatch);
});
function checkPasswordMatch() {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword)
        $("#divCheckPasswordMatch").html("Passwords do not match!");
    else
        $("#divCheckPasswordMatch").html("Passwords match.");
}
</script>
</head>
<body>
<div class="td">
    <input type="password" id="txtNewPassword" />
</div>
<div class="td">
    <input type="password" id="txtConfirmPassword"  />
</div>
    <div class="registrationFormAlert" id="divCheckPasswordMatch">
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您必须包含对JQuery的引用才能使用它。

<!DOCTYPE html>
<html>
<head>
<title>password change </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
   $("#txtConfirmPassword").keyup(checkPasswordMatch);
});
function checkPasswordMatch() {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword)
        $("#divCheckPasswordMatch").html("Passwords do not match!");
    else
        $("#divCheckPasswordMatch").html("Passwords match.");
}
</script>
</head>
<body>
<div class="td">
    <input type="password" id="txtNewPassword" />
</div>
<div class="td">
    <input type="password" id="txtConfirmPassword"  />
</div>
    <div class="registrationFormAlert" id="divCheckPasswordMatch">
</div>
</body>
</html>