我想限制用户输入 6-20 个字符范围内的密码。但下面的功能无法正常工作。
function validatePass(pwd1, pwd2)
{
var len = document.getElementById('pwd1').value;
if(len.length < 6 || len.length > 20)
{ pwd1.setCustomValidity('Enter 6-20 characters');
}
else if (pwd1.value != pwd2.value || pwd1.value == '' || pwd2.value == '' )
{
pwd2.setCustomValidity('Password incorrect');
}
else
{
pwd2.setCustomValidity('');
}
}
html代码是:
<h4>Password</h4></td>
<td><input type="password" name="pwd1" size="30" id="pwd1" required placeholder="Enter 6-20 characters"></td></tr>
<tr><td>
<h4>Confirm Password</h4></td>
<td><input type="password" name="pwd2" id="pwd2" size="30" required onfocus="validatePass(document.getElementById('pwd1'), this);" oninput="validatePass(document.getElementById('pwd1'), this);">
</td></tr>