密码验证脚本不工作

password validation script is not working

本文关键字:工作 脚本 验证 密码      更新时间:2023-09-26

我使用以下脚本验证密码。验证的目的是:

  1. 密码字段不能为空
  2. 密码长度应在6到10个字符之间
  3. 密码不包含空格和特殊字符
  4. 密码必须是字母数字。
但是使用下面的代码,它通过了前3个目标,但即使在输入字母数字文本后,它仍然警告:

"密码应同时包含字母和数字".

需要你的帮助

代码为:

if(document.subForm.password.value==""){
  alert("Please Enter Your Desired Password....");
  document.subForm.password.focus();
  return false;
}
if(document.subForm.password.value.length < 6 || document.subForm.password.value.length > 10){
  alert("Password Length Should Be In Between 6 And 10 Characters.");
  document.subForm.password.focus();
  return false;
}
var re = /^['w'A-Z]+$/;
if(!re.test(document.subForm.password.value)) {
  alert ("Your Password Has Spaces In Between The Words 'n'nOr'n'nIt Contains Special Characters.'n'nThese Are Not Allowed.'n'nPlease Remove Them And Try Again.");
  document.subForm.password.focus();
  return false;
}
var realphanumeric = /^[a-z_A-Z_0-9]+$/;
if (!realphanumeric.test(document.subForm.password.value)){ 
  alert("Password Should Contain Alphabet And Numbers Both");
  document.subForm.password.focus();
  return false;
}

Aragon0建议使用dropbox的开源脚本来检查密码强度。我建议你去看看。


如果你想用一个正则表达式检查所有内容:

^'w{6,10}$

解释:

  1. 从字符串的开始(^)到结束($)…
  2. 只匹配字母数字字符([A-Za-z_0-9]),
  3. ,长度为6-10个字符({6-10})

如果你想强制用户至少有一个数字,你可以这样做:

^(?![A-Za-z_]+$)'w{6,10}$

您的正则表达式

/^[a-z_A-Z_0-9]+$/

不能做你想要的。它将匹配密码"Test",但不匹配密码"te@st"。

您可以使用两个正则表达式,它们都需要匹配:

/[a-zA-Z]+/
/[0-9]+/

顺便说一句,您不应该强制执行字母数字密码或长度限制。你可以使用Dropbox的密码强度脚本(https://github.com/dropbox/zxcvbn)zxcvbn:

的一些示例代码
<script src="//cdn.jsdelivr.net/zxcvbn/1.0/zxcvbn-async.js" />
<script>
var result = zxcvbn(document.subForm.password.value);
if(result.entropy<56) // 56 is very secure, you could also lower it to 48 if you need to.
{
    alert("Your password is too weak. It would be cracked " + result.crack_time_display);
    return false;
}
</script>