-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseraccounts-password-strength.js
74 lines (64 loc) · 1.73 KB
/
useraccounts-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
import {
AccountsTemplates
} from 'meteor/useraccounts:core';
import {
Template
} from 'meteor/templating';
AccountsTemplates.removeField('password');
AccountsTemplates.addField({
_id: 'password',
type: 'password',
minLength: 8,
required: true,
func: function(value) {
var self = this;
if (Meteor.isClient) {
var result = getPasswordStrength(value);
if (result.strength !== 4) {
return true; // true means NOT valid
} else {
self.setSuccess();
}
}
},
errStr: 'Does not meet the requirements',
template: 'atTextInputWithPasswordStrength'
});
Accounts.passwordStrength = {
rules: {
upperCase: true,
lowerCase: true,
digits: true,
specialCharacters: true
},
strengthTextArr: [
"",
"Low",
"Medium",
"High",
"Strong"
]
};
export function getPasswordStrength(password) {
check(password, String);
var strength = 0;
if (password.trim().length !== 0) {
// Regular Expressions
var regex = new Array();
regex.push("[A-Z]"); // Uppercase Alphabet.
regex.push("[a-z]"); // Lowercase Alphabet.
regex.push("[0-9]"); // Digit.
regex.push("[$@$!%*#?&]"); // Special Character.
// Validate for each Regular Expression
for (var i = 0; i < regex.length; i++) {
if (new RegExp(regex[i]).test(password)) {
strength++;
}
}
}
return {
strength: strength,
strengthText: strength ? "Strength: " + Accounts.passwordStrength.strengthTextArr[strength] : '',
isAtRequiredStrength: strength === 4
};
}