-
Notifications
You must be signed in to change notification settings - Fork 10
/
jquery.password_strength.js
152 lines (133 loc) · 3.27 KB
/
jquery.password_strength.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
/*
* Password Strength (0.1.4)
* by Sagie Maoz (n0nick.net)
*
* This plugin will check the value of a password field and evaluate the
* strength of the typed password. This is done by checking for
* the diversity of character types: numbers, lowercase and uppercase
* letters and special characters.
*
* Copyright (c) 2010 Sagie Maoz <[email protected]>
* Licensed under the GPL license, see http://www.gnu.org/licenses/gpl-3.0.html
*
*
* NOTE: This script requires jQuery to work. Download jQuery at www.jquery.com
*
*/
(function($){
var passwordStrength = new function()
{
this.countRegexp = function(val, rex)
{
var match = val.match(rex);
return match ? match.length : 0;
};
this.getStrength = function(val, minLength)
{
var len = val.length;
// too short =(
if (len < minLength)
{
return 0;
}
var nums = this.countRegexp(val, /\d/g),
lowers = this.countRegexp(val, /[a-z]/g),
uppers = this.countRegexp(val, /[A-Z]/g),
specials = len - nums - lowers - uppers;
// just one type of characters =(
if (nums == len || lowers == len || uppers == len || specials == len)
{
return 1;
}
var strength = 0;
if (nums) { strength+= 2; }
if (lowers) { strength+= uppers? 4 : 3; }
if (uppers) { strength+= lowers? 4 : 3; }
if (specials) { strength+= 5; }
if (len > 10) { strength+= 1; }
return strength;
};
this.getStrengthLevel = function(val, minLength)
{
var strength = this.getStrength(val, minLength);
val = 1;
if (strength <= 0) {
val = 1;
} else if (strength > 0 && strength <= 4) {
val = 2;
} else if (strength > 4 && strength <= 8) {
val = 3;
} else if (strength > 8 && strength <= 12) {
val = 4;
} else if (strength > 12) {
val = 5;
}
return val;
};
};
$.fn.password_strength = function(options)
{
var settings = $.extend({
'container' : null,
'bar': null, // thanks codemonkeyking
'minLength' : 6,
'texts' : {
1 : 'Too weak',
2 : 'Weak password',
3 : 'Normal strength',
4 : 'Strong password',
5 : 'Very strong password'
},
'onCheck': null
}, options);
return this.each(function()
{
var container = null, $bar = null;
if (settings.container)
{
container = $(settings.container);
}
else
{
container = $('<span/>').attr('class', 'password_strength');
$(this).after(container);
}
if (settings.bar)
{
$bar = $(settings.bar);
}
$(this).bind('keyup.password_strength', function()
{
var val = $(this).val(),
level = passwordStrength.getStrengthLevel(val, settings.minLength);
if (val.length > 0)
{
var _class = 'password_strength_' + level,
_barClass = 'password_bar_' + level;
if (!container.hasClass(_class) && level in settings.texts)
{
container.text(settings.texts[level]).attr('class', 'password_strength ' + _class);
}
if ($bar && !$bar.hasClass(_barClass))
{
$bar.attr('class', 'password_bar ' + _barClass);
}
}
else
{
container.text('').attr('class', 'password_strength');
if ($bar) {
$bar.attr('class', 'password_bar');
}
}
if (settings.onCheck) {
settings.onCheck.call(this, level);
}
});
if ($(this).val() != '') { // thanks Jason Judge
$(this).trigger('keyup.password_strength');
}
});
};
})(jQuery);