我想通过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 时,请使用这些功能。我做了以下更改:
- 使代码不引人注目。
- 检查
blur()
的正确性。 - 仅在用户输入内容时进行检查。
- 删除了不必要的大括号。
- 添加了反向条件。
试试这个方法:
$(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>
关于javascript - onkeypress 上的密码不匹配警报 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32360136/