强度计算函数:
01/**
02 * Calculates the strength of a password
03 * @param {Object} p The password that needs to be calculated
04 * @return {int} intScore The strength score of the password
05 */
06 var calcStrength = function(p) {
07 var intScore = 0;
08 // PASSWORD LENGTH
09 intScore += p.length;
10
11 if(p.length > 0 && p.length <= 4) { // length 4 or less
12 intScore += p.length;
13 }
14 else if (p.length >= 5 && p.length <= 7) { // length between 5 and 7
15 intScore += 6;
16 }
17 else if (p.length >= 8 && p.length <= 15) { // length between 8 and 15
18 intScore += 12;
19 //alert(intScore);
20 }
21 else if (p.length >= 16) { // length 16 or more
22 intScore += 18;
23 //alert(intScore);
24 }
25
26 // LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
27 if (p.match(/[a-z]/)) { // [verified] at least one lower case letter
28 intScore += 1;
29 }
30 if (p.match(/[A-Z]/)) { // [verified] at least one upper case letter
31 intScore += 5;
32 }
33 // NUMBERS
34 if (p.match(/\d/)) { // [verified] at least one number
35 intScore += 5;
36 }
37 if (p.match(/.*\d.*\d.*\d/)) { // [verified] at least three numbers
38 intScore += 5;
39 }
40
41 // SPECIAL CHAR
42 if (p.match(/[!,@,#,$,%,^,&,*,?,_,~]/)) { // [verified] at least one special character
43 intScore += 5;
44 }
45 // [verified] at least two special characters
46 if (p.match(/.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~]/)) {
47 intScore += 5;
48 }
49
50 // COMBOS
51 if (p.match(/(?=.*[a-z])(?=.*[A-Z])/)) { // [verified] both upper and lower case
52 intScore += 2;
53 }
54 if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/)) { // [verified] both letters and numbers
55 intScore += 2;
56 }
57 // [verified] letters, numbers, and special characters
58 if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!,@,#,$,%,^,&,*,?,_,~])/)) {
59 intScore += 2;
60 }
61 return intScore;
62};
02 * Calculates the strength of a password
03 * @param {Object} p The password that needs to be calculated
04 * @return {int} intScore The strength score of the password
05 */
06 var calcStrength = function(p) {
07 var intScore = 0;
08 // PASSWORD LENGTH
09 intScore += p.length;
10
11 if(p.length > 0 && p.length <= 4) { // length 4 or less
12 intScore += p.length;
13 }
14 else if (p.length >= 5 && p.length <= 7) { // length between 5 and 7
15 intScore += 6;
16 }
17 else if (p.length >= 8 && p.length <= 15) { // length between 8 and 15
18 intScore += 12;
19 //alert(intScore);
20 }
21 else if (p.length >= 16) { // length 16 or more
22 intScore += 18;
23 //alert(intScore);
24 }
25
26 // LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
27 if (p.match(/[a-z]/)) { // [verified] at least one lower case letter
28 intScore += 1;
29 }
30 if (p.match(/[A-Z]/)) { // [verified] at least one upper case letter
31 intScore += 5;
32 }
33 // NUMBERS
34 if (p.match(/\d/)) { // [verified] at least one number
35 intScore += 5;
36 }
37 if (p.match(/.*\d.*\d.*\d/)) { // [verified] at least three numbers
38 intScore += 5;
39 }
40
41 // SPECIAL CHAR
42 if (p.match(/[!,@,#,$,%,^,&,*,?,_,~]/)) { // [verified] at least one special character
43 intScore += 5;
44 }
45 // [verified] at least two special characters
46 if (p.match(/.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~]/)) {
47 intScore += 5;
48 }
49
50 // COMBOS
51 if (p.match(/(?=.*[a-z])(?=.*[A-Z])/)) { // [verified] both upper and lower case
52 intScore += 2;
53 }
54 if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/)) { // [verified] both letters and numbers
55 intScore += 2;
56 }
57 // [verified] letters, numbers, and special characters
58 if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!,@,#,$,%,^,&,*,?,_,~])/)) {
59 intScore += 2;
60 }
61 return intScore;
62};
使用:
<input type="password" name="data[Shopkeeper][password]" value="" id="ShopkeeperPassword" tabindex="3″ maxlength="32″/>
JS:
var grade = calcStrength(this.value);//即可得到密码强度值
//我自己用的强度等级
var gradeMsg = grade <=0 ? " : (grade < 23 ? '弱' : (grade < 40 ? '一般' : (grade < 60 ? '强' : '极强'))) ;
var gradeMsg = grade <=0 ? " : (grade < 23 ? '弱' : (grade < 40 ? '一般' : (grade < 60 ? '强' : '极强'))) ;
没有评论:
发表评论