【发布时间】:2015-01-23 06:12:19
【问题描述】:
大家好,我有一个密码验证器,我正在处理它的问题,它很长,我认为如果可能的话可以缩短和简化。
有人可以帮助我简化它。我说的是 checkValidPassword() 函数。
function check(input) {
if (input.value != document.getElementById('password').value) {
input.setCustomValidity('Password Must be Matching.');
} else {
// input is valid -- reset the error message
input.setCustomValidity('');
// check the length of the password
checkValidPassword(input);
}
}
function checkValidPassword(input) {
var password = document.getElementById('password');
var confirm_password = document.getElementById('confirm password');
if (password.value.length < 8) {
password.setCustomValidity('Password must contain at least 8 characters!');
} else {
var re = /[0-9]/;
if (!re.test(password.value)) {
password.setCustomValidity('password must contain at least one number (0-9)!');
} else {
password.setCustomValidity("");
}
}
}
我试图实现一种方式,让用户也必须至少包含一个数字。我在想
str.match(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8,})$/)
我是否可以在 if 语句中使用 $$ 来表示 并检查字符 吗?
if(password.value.length < 8 && str.match(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8,})$/)) {
【问题讨论】:
标签: javascript validation passwords