简体   繁体   English

HTML5自定义验证无法正常工作

[英]HTML5 custom validation not working properly

i've the following function to verify matching between 2 password input fields. 我具有以下功能来验证2个密码输入字段之间的匹配。 When i submit the form even if the password match i got the error and i don't understand where is the problem. 当我提交表单时,即使密码匹配,也会出现错误,而且我不明白问题出在哪里。 Can you guys have a look? 你们可以看看吗? thanks 谢谢

function checkPwdMatch() {

if (document.getElementById('password').value !=document.getElementById('confirm_password').value) {
document.getElementById('confirm_password').setCustomValidity('Two Passwords must match.');
} else {
document.getElementById('confirm_password').setCustomValidity('');
}

}

----FORM FIELDS---- ----表格字段----

<input class="form-control" type="password" placeholder="Password" id="password" name="password" value="" required>

<input class="form-control" type="password" placeholder="<?php echo $lang["joinRepeatPassword"]?>" value="" id="confirm_password" name="confirm_password"  required>

Are you sure there are not other elements with the same id (password or confirm_password) in your html code? 您确定HTML代码中没有其他具有相同ID(密码或Confirm_Password)的元素吗?


When I have such html 当我有这样的HTML

<!doctype html>
<html>
<head>
<script>


function checkPwdMatch() {

if (document.getElementById('password').value !=document.getElementById('confirm_password').value) {
document.getElementById('confirm_password').setCustomValidity('Two Passwords must match.');
} else {
document.getElementById('confirm_password').setCustomValidity('');
}

}
</script>
</head>
<body>
<form method="post">
<input class="form-control" type="password" placeholder="Password" id="password" name="password" value="" required>

<input class="form-control" type="password" placeholder="password copy" value="" id="confirm_password" name="confirm_password"  onblur="checkPwdMatch()"  required>


<input type="submit" />
</form>
</body>
</html>

if I put aaa and bbb in two inputs, it shows they are invalid and when i put aaa and aaa everything seems to be ok. 如果我将aaa和bbb放在两个输入中,则表明它们是无效的,当我将aaa和aaa放入时,一切似乎正常。 If the problem is after you send form and page refresh you need to send more details I think. 如果问题出在您发送表格和页面刷新后,您需要发送更多详细信息,我认为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM