首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JS密码表检查

JS密码表检查
EN

Stack Overflow用户
提问于 2020-01-10 17:21:56
回答 2查看 96关注 0票数 2

我一直在玩这个,因为某些原因无法得到所期望的行为。下面的代码已被多次修改和重新排列,但要求我多次单击submit,并且它只显示第一个有效性消息,即使条件不满足。

代码语言:javascript
运行
复制
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;
  }
};
代码语言:javascript
运行
复制
<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一次,如果旧密码与新密码相同,或者新密码不匹配,则抛出一条消息。否则,提交表单。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-10 18:12:19

这是解决办法,似乎是正确的。

代码语言:javascript
运行
复制
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;
  }
};
代码语言:javascript
运行
复制
<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>

票数 1
EN

Stack Overflow用户

发布于 2020-01-10 17:28:13

一件事是,您需要使用<input type="submit" onclick="return check()">,而另一件事是,您需要在check函数中的条件中的任何地方使用return false;,如果必须不发送它的话。如果一切都是正确的,只需return true,然后表单将提交。

代码语言:javascript
运行
复制
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;
	}
}
代码语言:javascript
运行
复制
<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>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59686310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档