0

嗨,我有一个密码检查器,但我不知道我应该为我的程序使用什么代码。我需要我的程序来检查规则,但我不知道该怎么做。我希望有人会知道如何做到这一点,或者至少帮助我这样做。这些是我的程序的亮点。

用户输入他们想要的密码,系统会根据一系列检查告诉他们他们的密码是否是强密码。要使密码被认为是强密码,它必须通过以下测试: 密码必须至少包含 1 个大写字母 密码必须至少包含 1 个小写字母 密码必须至少包含 1 个数字 密码必须至少包含 1 个以下字符:! @ # $ % ^ & 密码长度必须至少为 8 个字符 当用户键入时,应向他们提供有关密码强度的反馈。您将需要检查每个字母,以查看密码是否符合这些规则。

这是我的 HTML。

<div style="text-align: center;" style="font-weight:bold;"> 
<h1> Password Strength Checker</h1>
<br/>
The most secure passwords will have a very strong rating.
<br/>
The ratings scale range from: Weak (0), Good (25) , Medium (50), Strong (75) and Very Strong (100).
<br/>
<br/>
RULES:
<br/>
1. Your password must contain at least 1 number.
<br/>
2. Your password must be at least 8 characters long.
<br/>
3. Your password must contain at least 1 capital letter.
<br/>
4. Your password must contain at least 1 lowercase letter.
<br/>
5. Your password must contain at least 1 of the following characters:
<br/>! @ + # $ % ^ & * , ? _ ~ - ( )  
<br/>
Spaces are not allowed.
<br/>
<br/>
<input type="text" id="password" size="30" name="password" autocomplete="off" onkeydown="passwordStrength(this.value);"><span id="passwordStrength" class="strength0"><span id = "passwordDescription"><br/>
Please Enter Password <br/>

这是我的 JavaScript。

 function passwordStrength(password) {
 var rating = [
 0, "<font color='black'> Weak </font>",
 25, "<font color='red'> Good </font>",
 50, "<font color='yellow'> Medium </font>",
 75, "<font color='blue'> Strong </font>",
 100, "<font color='green'> Very Strong </font>"];

 var score = 0;
 var pass = "";

 if (password.length > 8) {
     score += 25;
 }      
 if ((password.match(/[a-z]/)) && (password.match(/[A-Z]/))) {
     score += 25
 }
 if (password.match(/.[,!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)) {
     score += 25;
 }
 if (password.match(/[0-9]/)) {
     score += 25
 }
 if (password.match(/d+/)) {
     score += 10;}

 for (var i = rating.length - 1; i >= 0; i -= 1) {
     if (score >= rating[i]) {
         pass = rating[i +1];
         break;
     }
 }
 document.getElementById("passwordDescription").innerHTML = "<b>" + pass + score + "</font></b>"
 document.getElementById("passwordStrength").className = "strength" + score;
 }

这是我的jsfiddle。http://jsfiddle.net/jYcBT/138/

4

1 回答 1

1

不确定您的问题是什么?

但是,我刚刚修复了您的特殊字符正则表达式

password.match(/[\!\@\#\$\%\^\&\*\?\_\~\-\(\)]+/) 

删除您拥有的额外 10 分,不知道为什么要添加这个?

将事件从 onkeydown 更改为 onchange。

据我了解您的流程,它工作正常。

http://jsfiddle.net/jYcBT/140/

于 2014-09-15T02:30:34.390 回答