密码不匹配警报javascript上的onkeypress


password mismatch alert javascript on onkeypress

我想通过onkeypress()函数检查确认密码是否与密码匹配。不管用……一些部分工作,如密码长度必须是6个字符。但是,我有问题…当我输入确认密码的值…即使在它与我在密码文本中输入的密码值匹配之后,它也显示div错误消息。请帮助。这是我的代码。

<input type='password' class='Register-textbox Password' name="Password" onkeypress="RegistrationValidation()">
<input type='password' class='Register-textbox ConfirmPassword' name="ConfirmPassword"  onkeypress="RegistrationValidation()">
<div class="ShowPasswordNotMatchesError" style="display:none;">
  * Password Mismatch
</div>
<script>
function RegistrationValidation()
{
       var PasswordVal=$('.Password').val();
       var ConfirmPasswordVal=$('.ConfirmPassword').val();
       if(PasswordVal!=ConfirmPasswordVal)
       {
        $('.ShowPasswordNotMatchesError').show();
        }
}
</script>

在使用jQuery时,请使用这些函数。我做了以下修改:

  1. 使代码不显眼。
  2. 检查blur()的正确性。
  3. 只在用户输入内容时检查。
  4. 删除不必要的括号。
  5. 添加反向条件。

试试这样:

$(function () {
  $(".Register-textbox").blur(function () {
    var PasswordVal=$('.Password').val();
    var ConfirmPasswordVal=$('.ConfirmPassword').val();
    if(PasswordVal != ConfirmPasswordVal && ConfirmPasswordVal.length > 0 && PasswordVal.length > 0)
      $('.ShowPasswordNotMatchesError').show();
    else
      $('.ShowPasswordNotMatchesError').hide();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type='password' class='Register-textbox Password' name="Password" />
<input type='password' class='Register-textbox ConfirmPassword' name="ConfirmPassword" />
<div class="ShowPasswordNotMatchesError" style="display:none;">
  * Password Mismatch
</div>