我有一个密码并确认密码字段,如下所示。在此处输入图像描述
我在其他两种不同的形式中使用了相同的元素和脚本,但它不适用于其中一种形式。
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"> *</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"> *</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>