-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.passwordcomplexity.js
More file actions
183 lines (144 loc) · 4.32 KB
/
jquery.passwordcomplexity.js
File metadata and controls
183 lines (144 loc) · 4.32 KB
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
$(function(){
"use strict";
function calculate()
{
function isContainsLowerCaseLetters(value) {
return (/[a-z]/).test(value);
}
function isContainsUpperCaseLettes(value) {
return (/[A-Z]/).test(value);
}
function isContainsSpaces (value) {
return (/ /).test(value);
}
function isContainsNumbers (value) {
return (/[0-9]/).test(value);
}
function isContainsSpecialSymbols (value) {
var contains = false,
symbols = "-!§$%&/()=?.:,~;'#+-/*\\|{}[]_<>\"".split("");
$.each(symbols, function(index, symbol){
if (value.indexOf(symbol)) {
contains = true;
// We found a character. Return false is needed for exit from loop
return false;
};
})
return contains;
}
function countOfSpaces (value) {
return value.split(/ +/).length - 1;
}
return {
estimate: function(value, points){
var score = value.length * points.forEachCharacter;
if (isContainsSpaces(value)) { score += countOfSpaces(value) * points.forEachSpace; }
if (isContainsLowerCaseLetters(value)) { score += points.containsLowerCaseLetter; }
if (isContainsUpperCaseLettes(value)) { score += points.containsUpperCaseLetter; }
if (isContainsNumbers(value)) {score += points.containsNumber; }
if (isContainsSpecialSymbols(value)) { score += points.containsSpecialSymbols};
return score;
}
};
}
function Indicate (indicator, settings) {
var $indicator = $(indicator).hide();
function getSecurityClass (score) {
var index = parseInt(Math.round(score * (settings.secureClassNames.length - 1) * 100 / settings.secureStrenght) / 100, 10);
if (index >= settings.secureClassNames.length) {
index = settings.secureClassNames.length -1;
}
return settings.secureClassNames[index];
}
return {
refresh: function(score){
if (score > 0) {
$indicator.css("display", settings.indicatorDisplayType);
} else {
$indicator.hide();
}
var secureClass = getSecurityClass(score);
$.each(settings.secureClassNames, function (index, value){
$indicator.removeClass(value.name);
});
$indicator.addClass(secureClass.name);
if (settings.text) {
$indicator.text(secureClass.text);
}
}
};
}
var calculator,
defaults = {
secureStrenght : 25,
$indicator: undefined,
indicatorClassName: "password-secure-indicator",
indicatorDisplayType: "inline-block",
text: true,
points: {
forEachCharacter: 1,
forEachSpace: 1,
containsLowerCaseLetter: 2,
containsUpperCaseLetter:2,
containsNumber: 4,
containsSpecialSymbols :5
},
secureClassNames :[{
name: "very-weak",
text: "very weak"
}, {
name: "weak",
text: "weak"
}, {
name: "mediocre",
text: "mediocre"
}, {
name: "strong",
text: "strong"
}, {
name: "very-strong",
text: "very strong"
}]
},
methods = {
init: function (options){
var settings = $.extend({}, defaults, options),
$input = $(this),
$indicator = getIndicatorElement($input, settings),
indicator = new Indicate($indicator, settings);
setupAutomaticIndicatorRefresh(indicator, $input, settings);
return $input;
},
calculate: function(value, options){
var settings = $.extend(defaults, options);
if (!calculator) {
calculator = new calculate();
}
return calculator.estimate(value, settings.points);
},
defaults: function(){
return defaults;
}
};
function getIndicatorElement ($input, settings) {
var $indicator = settings.$indicator || $("<span> </span>").insertAfter($input);
return $indicator.attr("class", settings.indicatorClassName);
}
function setupAutomaticIndicatorRefresh (indicator, $input, settings) {
var refresh = function(){
var password = $input.val(),
score = methods.calculate(password, settings);
indicator.refresh(score);
};
$input.on("keyup", refresh);
}
$.fn.passwordComplexity = $.fn.passwordComplexity = function(method){
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
if (typeof method == "object" || !method) {
return methods.init.apply(this, arguments);
}
$.error("Method "+ method + "does not exist on JQuery.passwordComplexity");
};
});