我一直在玩这个,因为某些原因无法得到所期望的行为。下面的代码已被多次修改和重新排列,但要求我多次单击submit
,并且它只显示第一个有效性消息,即使条件不满足。
function check() {
input1 = document.getElementById('old_pass')
input2 = document.getElementById('new_pass')
input3 = document.getElementById('new_passV')
if (input2.value !== input1.value && input3.value === input2.value) {
$('#pass_form').submit();
} else if (input2.value === input1.value) {
input2.setCustomValidity('Your new password cannot match your old password.');
} else if (input3.value !== input2.value) {
input3.setCustomValidity('New password must be matching.');
} else {
input2.setCustomValidity('');
input3.setCustomValidity('');
return true;
}
};
<form id="pass_form" action="./php/pass-conn.php" method="POST">
<br>
Enter Current Password:
<br>
<input type="password" name="old_pass" id="old_pass" title="Password must contain at least eight characters" autofocus="autofocus">
<br><br><br>
Enter New Password:
<br>
<input type="password" name="new_pass" id="new_pass" pattern=".{8,}" title="Password must contain at least eight characters">
<br><br>
Confirm New Password:
<br>
<input type="password" name="new_passV" id="new_passV" pattern=".{8,}" title="Password must contain at least eight characters">
<br><br>
<a href="javascript:void(0);" id="showpass" onclick="showpass()">
<u>Show/Hide</u>
</a>
<br><br>
<input type="submit" onclick="check()">
</form>
所需的行为是让用户单击submit
一次,如果旧密码与新密码相同,或者新密码不匹配,则抛出一条消息。否则,提交表单。
发布于 2020-01-10 18:12:19
这是解决办法,似乎是正确的。
function check(){
input1 = document.getElementById('old_pass')
input2 = document.getElementById('new_pass')
input3 = document.getElementById('new_passV')
input2.setCustomValidity('');
input3.setCustomValidity('');
if(input2.value !== input1.value && input3.value === input2.value){
return true;
}else if(input2.value === input1.value){
input2.setCustomValidity('Your new password cannot match your old password.');
return false;
}else if(input3.value !== input2.value){
input3.setCustomValidity('New password must be matching.');
return false;
}
};
<form id="pass_form" method="POST"><br>
Enter Current Password: <br><input type="password" name="old_pass" id="old_pass" title="Password must contain at least eight characters" autofocus="autofocus"><br><br><br>
Enter New Password: <br><input type="password" name="new_pass" id="new_pass" pattern=".{8,}" title="Password must contain at least eight characters"><br><br>
Confirm New Password: <br><input type="password" name="new_passV" id="new_passV" pattern=".{8,}" title="Password must contain at least eight characters"><br><br>
<input type="submit" onclick="check()">
</form>
发布于 2020-01-10 17:28:13
一件事是,您需要使用<input type="submit" onclick="return check()">
,而另一件事是,您需要在check函数中的条件中的任何地方使用return false;
,如果必须不发送它的话。如果一切都是正确的,只需return true
,然后表单将提交。
function check() {
input1 = document.getElementById('old_pass');
input2 = document.getElementById('new_pass');
input3 = document.getElementById('new_passV');
if (input2.value !== input1.value && input3.value === input2.value) {
return true;
}
else if (input2.value === input1.value) {
input2.setCustomValidity('Your new password cannot match your old password.');
return false;
}
else if (input3.value !== input2.value) {
input3.setCustomValidity('New password must be matching.');
return false;
}
}
<form id="pass_form" action="./php/pass-conn.php" method="POST"><br>
Enter Current Password: <br><input type="password" name="old_pass" id="old_pass" title="Password must contain at least eight characters" autofocus="autofocus"><br><br><br>
Enter New Password: <br><input type="password" name="new_pass" id="new_pass" pattern=".{8,}" title="Password must contain at least eight characters"><br><br>
Confirm New Password: <br><input type="password" name="new_passV" id="new_passV" pattern=".{8,}" title="Password must contain at least eight characters"><br><br>
<a href="javascript:void(0);" id="showpass" onclick="showpass()"><u>Show/Hide</u></a><br><br>
<input type="submit" onclick="return check()">
</form>
https://stackoverflow.com/questions/59686310
复制相似问题