-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.passMeter.js
135 lines (109 loc) · 4.97 KB
/
jquery.passMeter.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
/*!
* jquery.passMeter v1.0.3
* measure password strength
* https://github.com/RuanAragao/passMeter
* MIT License
* by Ruan Aragão
*/
; (function ($, window, document, undefined) {
$.passMeter = function (settings) {
var config = {
// Config local
id_password: '#password',
id_result: '#password-result',
id_submitButton: '#submitButton',
// Msg level pass
msg_bad: 'Bad',
msg_low: 'Low',
msg_good: 'Good',
msg_strong: 'Strong',
// configurable length
min_chars: 6,
min_chars_boost: 8,
// configurable strength check
min_strength_good: 2,
min_strength_strong: 3,
// configurable regex for special chars
regex_special_chars: /[!%&@#$\^*?_~]/,
// at least 2 special chars
regex_advanced: /.*[!%&@#$\^*?_~].*[!%&@#$\^*?_~].*/,
// callback function
callback: null,
};
if (settings) { settings = jQuery.extend(config, settings); }
// variables
var nodePassword = $(settings.id_password);
var nodeResult = $(settings.id_result);
var nodeSubmitButton = $(settings.id_submitButton);
// all our potential results for css
var resultClasses = Array('bad', 'low', 'good', 'strong');
nodePassword.keyup(function () {
var message = checkStrength(nodePassword.val());
//Calling method used to Block/Release submit button (used in "keyup" event so it reacts on keypress along with checkStrenth())
toggleSubmitButton(nodeSubmitButton, checkStrength(nodePassword.val()))
nodeResult.html(message);
if (settings.callback !== null) {
settings.callback(message);
}
});
/**
* Method used to Block/Release submit button
* @param {*} button - Button element that's going to be disabled
* @param {String} value - Return value of the method checkStrenth()
*/
function toggleSubmitButton(button, value) {
if (button.attr("disabled")) {
if (value === settings.msg_good || value === settings.msg_strong) {
button.removeAttr('disabled')
}
} else if (!button.attr("disabled")) {
if (value !== settings.msg_good && value !== settings.msg_strong) {
button.attr('disabled', true)
}
}
}
function checkStrength(password) {
//initial force
var strength = 0;
// remove existing label
for (var i = 0, len = resultClasses.length; i < len; i++) {
nodePassword.removeClass("passMeter-" + resultClasses[i]);
nodeResult.removeClass("passMeter-" + resultClasses[i]);
}
//if password length is less than settings.min_chars, return message.
if (password.length < settings.min_chars) {
nodePassword.addClass('passMeter-bad');
nodeResult.addClass('passMeter-bad');
return settings.msg_bad;
}
//if password length is greater than settings.min_chars, go.
//if password length is settings.min_chars_boost characters or more, increase force value.
if (password.length >= settings.min_chars_boost) strength += 1;
//if password contains both lower and uppercase characters, increase strength value
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) strength += 1;
//if it has numbers and characters, increase strength value
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) strength += 1;
//if it has one special character, increase strength value
if (password.match(settings.regex_special_chars)) strength += 1;
//if it matches the advanced regex, increase strength value
if (password.match(settings.regex_advanced)) strength += 1;
console.log(password.length, strength);
//now we have calculated strength value, we can return messages
//if value is less than 2
if (strength < settings.min_strength_good) {
nodePassword.addClass('passMeter-low');
nodeResult.addClass('passMeter-low');
return settings.msg_low;
} else if ((strength >= settings.min_strength_good) && (strength < settings.min_strength_strong)) {
nodePassword.addClass('passMeter-good');
nodeResult.addClass('passMeter-good');
return settings.msg_good;
} else {
nodePassword.addClass('passMeter-strong');
nodeResult.addClass('passMeter-strong');
return settings.msg_strong;
}
}
return this;
};
})(jQuery, window, document);