forked from jvortmann/passwordStrength.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passwordStrength.js
192 lines (155 loc) · 5.05 KB
/
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
!(function(moduleName, definition) {
// Whether to expose PasswordStrength as an AMD module
// or to the global object.
if (typeof define === 'function' && typeof define.amd === 'object') define(definition);
else this[moduleName] = definition();
})('passwordStrength', function definition() {
var defaultDefinitions = (function(){
function getFlatScore(password, regex, bonus) {
var score = 0;
for (var i = 0; i < password.length; i += 1) {
if (regex.test(password[i])) {
score = score + bonus;
}
}
return score;
}
function getConditionalIncrementalScore(password, regex, bonus) {
var score = getFlatScore(password, regex, bonus);
if (score === 0) { return 0};
return (password.length - score) * 2;
}
var passwordSize = function(password) {
return password.length * 4;
};
var containsNumber = function(password) {
return getFlatScore(password, /\d/, 4);
};
var containsLowercaseLetter = function(password) {
return getConditionalIncrementalScore(password, /[a-z]/, 1);
};
var containsUppercaseLetter = function(password) {
return getConditionalIncrementalScore(password, /[A-Z]/, 1);
};
var containsSymbols = function(password) {
return getFlatScore(password, /\W/, 6);
};
return {
limits : { none: 0, weak: 1, good: 29, strong: 59, veryStrong: 89},
requirements: [ passwordSize, containsNumber, containsLowercaseLetter, containsUppercaseLetter, containsSymbols ]
}
}());
var status = function(definitions) {
function innerTextNode(text, status) {
var element = document.createElement("span");
element.className = "score";
element.innerHTML = text;
element.classList.add(status);
return element;
};
function none() {
return innerTextNode("None", "none");
};
function weak() {
return innerTextNode("Weak", "weak");
};
function good() {
return innerTextNode("Good", "good");
};
function strong() {
return innerTextNode("Strong", "strong");
};
function veryStrong() {
return innerTextNode("Very Strong", "very_strong");
};
function show(parent) {
var element_score = parent.getElementsByClassName("score")[0];
element_score.classList.remove("hidden");
};
function hide(parent) {
var element_score = parent.getElementsByClassName("score")[0];
element_score.classList.add("hidden");
};
function replace(parent, newStatus) {
var elementStrength = parent.getElementsByClassName("password_strength")[0];
var elementStatus = elementStrength.getElementsByClassName("score")[0];
elementStrength.removeChild(elementStatus);
elementStrength.appendChild(newStatus);
};
function update(parent, rate) {
if (rate >= definitions.limits.veryStrong) {
replace(parent, veryStrong());
} else if (rate >= definitions.limits.strong) {
replace(parent, strong());
} else if (rate >= definitions.limits.good){
replace(parent, good());
} else if (rate >= definitions.limits.weak){
replace(parent, weak());
} else {
replace(parent, none());
}
};
return {
none : none,
show: show,
hide: hide,
update: update
}
};
var score = function(definitions) {
function calculate(password) {
var rate = 0;
for (var i = 0; i < definitions.requirements.length; i += 1) {
rate = rate + definitions.requirements[i](password);
}
return rate;
}
return {
calculate: calculate,
};
};
var wrapper = function(status) {
return {
passwordStrength : function() {
var inner = status.none();
inner.classList.add("hidden");
var wrapper = document.createElement("div");
wrapper.className = "password_strength";
wrapper.appendChild(inner);
return wrapper;
}
};
};
var dom = function(element) {
function parentOf(element) {
return element.parentNode;
};
function addWrapperToParent(status){
parentOf(element).appendChild(wrapper(status).passwordStrength());
};
function registerEvents(status, score){
element.addEventListener('focusin', function(){
status.show(parentOf(element));
});
element.addEventListener('focusout', function(){
status.hide(parentOf(element));
});
element.addEventListener('input', function(){
var rate = score.calculate(element.value);
status.update(parentOf(element), rate);
});
};
return {
addWrapperToParent : addWrapperToParent,
registerEvents : registerEvents
}
};
function passwordStrength(element, customDefinitions) {
var definitions = customDefinitions || defaultDefinitions;
var definedStatus = status(definitions);
var domElement = dom(element);
domElement.addWrapperToParent(definedStatus);
domElement.registerEvents(definedStatus, score(definitions));
};
return passwordStrength;
});