密码和确认密码不匹配

时间:2017-03-04 02:16:18

标签: javascript html5 html5-validation

我有密码并确认密码字段,如下所示。enter image description here

我在其他两种不同的形式中使用了相同的元素和脚本,但它没有使用其中一种形式。

var password = document.getElementById('pwd'), confirm_password = document.getElementById('cpwd');
	function validatePassword() {
		if (password.value != confirm_password.value) {
			confirm_password.setCustomValidity('Passwords Don\'t Match');
		} else {
			confirm_password.setCustomValidity('');
		}
	}
	password.onchange = validatePassword;
	confirm_password.onkeyup = validatePassword;
<div class="input-field col s12 m6">
  <input id="pwd" name="pwd" type="password" class="validate" minlength="8" required="">
  <label for="pwd" data-error="Please enter a name of length minum 8 characters" data-success="Perfect!">Desired Password<span class="red-text">&nbsp;*</span></label>
</div>
					
<div class="input-field col s12 m6">
  <input id="cpwd" name="cpwd" type="password" class="validate"  required="">
  <label for="cpwd" data-error="Please enter the same password again" data-success="Perfect!">Confirm password<span class="red-text">&nbsp;*</span></label>
</div>


  <!-- Compiled and minified CSS -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css">

      <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
      
  <!-- Compiled and minified JavaScript -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/js/materialize.min.js"></script>

3 个答案:

答案 0 :(得分:0)

我建议您不要使用keyupchange事件来收听input事件。

来自the relevant article on MDN

  

input<input>元素的值发生更改时,会同步触发DOM <textarea>事件。

var password = document.getElementById('pwd'),
  confirm_password = document.getElementById('cpwd');

function validatePassword() {
  if (password.value !== confirm_password.value) {
    confirm_password.setCustomValidity("Passwords Don't Match");
  } else {
    confirm_password.setCustomValidity('');
  }
}
password.oninput = validatePassword;
confirm_password.oninput = validatePassword;
<div class="input-field col s12 m6">
  <input id="pwd" name="pwd" type="password" class="validate" minlength="8" required="">
  <label for="pwd" data-error="Please enter a name of length minum 8 characters" data-success="Perfect!">Desired Password<span class="red-text">&nbsp;*</span></label>
</div>

<div class="input-field col s12 m6">
  <input id="cpwd" name="cpwd" type="password" class="validate" required="">
  <label for="cpwd" data-error="Please enter the same password again" data-success="Perfect!">Confirm password<span class="red-text">&nbsp;*</span></label>
</div>

答案 1 :(得分:0)

Password: <input id="pwd" name="pwd" type="password"><br><br>
Conform Password: <input id="cpwd" name="cpwd" type="password">
<div id="errorMsg"></div>
<script>
var password = document.getElementById('pwd');
confirm_password = document.getElementById('cpwd');
    function validatePassword() {
        if ((confirm_password.value!='')&&(password.value != confirm_password.value)) {
            document.getElementById('errorMsg').innerHTML='Passwords Don\'t Match';
        } else {
            document.getElementById('errorMsg').innerHTML='';
        }
    }
    password.onchange = validatePassword;
    confirm_password.onkeyup = validatePassword;
</script>

答案 2 :(得分:0)

显然,这只是其余脚本中的一个类冲突,并且没有这样的问题。

不好意思。

感谢您的所有努力。