This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Ext.plugin.extjs.form.PasswordStrength.js
103 lines (83 loc) · 2.06 KB
/
Ext.plugin.extjs.form.PasswordStrength.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
Ext.define("Ext.plugin.extjs.form.PasswordStrength", {
extend : "Ext.AbstractPlugin",
alias : "plugin.passwordstrength",
colors : ["C11B17", "FDD017", "4AA02C", "6AFB92", "00FF00"],
init: function(cmp) {
var me = this;
cmp.on("change", me.onFieldChange, me);
},
onFieldChange: function(field, newVal, oldVal) {
if (newVal === "") {
field.inputEl.setStyle({
"background-color" : null,
"background-image" : null
});
field.score = 0;
return ;
}
var me = this,
score = me.scorePassword(newVal);
field.score = score;
me.processValue(field, score);
},
processValue: function(field, score) {
var me = this,
colors = me.colors,
color;
if (score < 16) {
color = colors[0]; //very weak
} else if (score > 15 && score < 25) {
color = colors[1]; //weak
} else if (score > 24 && score < 35) {
color = colors[2]; //mediocre
} else if (score > 34 && score < 45) {
color = colors[3]; //strong
} else {
color = colors[4]; //very strong
}
field.inputEl.setStyle({
"background-color" : "#" + color,
"background-image" : "none"
});
},
scorePassword: function(passwd) {
var score = 0;
if (passwd.length < 5) {
score += 3;
} else if (passwd.length > 4 && passwd.length < 8) {
score += 6;
} else if (passwd.length > 7 && passwd.length < 16) {
score += 12;
} else if (passwd.length > 15) {
score += 18;
}
if (passwd.match(/[a-z]/)) {
score += 1;
}
if (passwd.match(/[A-Z]/)) {
score += 5;
}
if (passwd.match(/\d+/)) {
score += 5;
}
if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/)) {
score += 5;
}
if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)) {
score += 5;
}
if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) {
score += 5;
}
if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) {
score += 2;
}
if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) {
score += 2;
}
if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)) {
score += 2;
}
return score;
}
});