forked from deanilvincent/check-password-strength
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (88 loc) · 2.32 KB
/
index.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
module.exports = (password) => {
const disallowedRegex = '(?=.*[£"\'\\\\])';
const lowerCaseRegex = "(?=.*[a-z])";
const upperCaseRegex = "(?=.*[A-Z])";
const numericRegex = "(?=.*[0-9])";
const symbolsRegex = '(?=.*[!#$%&()*+,\-.:;<=>?@\\][_\^`{|}~])';
let strength = {
id: null,
value: null,
length: null,
contains: [],
};
// Default
let passwordContains = [];
if (new RegExp(`^${lowerCaseRegex}`).test(password)) {
passwordContains = [
...passwordContains,
{
message: "lowercase",
},
];
}
if (new RegExp(`^${upperCaseRegex}`).test(password)) {
passwordContains = [
...passwordContains,
{
message: "uppercase",
},
];
}
if (new RegExp(`^${symbolsRegex}`).test(password)) {
passwordContains = [
...passwordContains,
{
message: "symbol",
},
];
}
if (new RegExp(`^${numericRegex}`).test(password)) {
passwordContains = [
...passwordContains,
{
message: "number",
},
];
}
if (new RegExp(`^${disallowedRegex}`).test(password)) {
passwordContains = [
...passwordContains,
{
message: "disallowed",
},
];
}
const extraStrongRegex = new RegExp(
`^${lowerCaseRegex}${upperCaseRegex}${numericRegex}${symbolsRegex}(?=.{8,})`
);
const strongRegex = new RegExp(
`^((${lowerCaseRegex}${upperCaseRegex}${numericRegex})|(${lowerCaseRegex}${upperCaseRegex}${symbolsRegex})|(${lowerCaseRegex}${numericRegex}${symbolsRegex})|(${upperCaseRegex}${numericRegex}${symbolsRegex}))(?=.{8,})`
);
const mediumRegex = new RegExp(
`^((${lowerCaseRegex}${upperCaseRegex})|(${lowerCaseRegex}${numericRegex})|(${upperCaseRegex}${numericRegex})|(${upperCaseRegex}${symbolsRegex})|(${lowerCaseRegex}${symbolsRegex})|(${numericRegex}${symbolsRegex}))(?=.{6,})`
);
if (extraStrongRegex.test(password)) {
strength = {
id: 3,
value: "Extra Strong",
};
} else if (strongRegex.test(password)) {
strength = {
id: 2,
value: "Strong",
};
} else if (mediumRegex.test(password)) {
strength = {
id: 1,
value: "Medium",
};
} else {
strength = {
id: 0,
value: "Weak",
};
}
strength.length = password.length;
strength.contains = passwordContains;
return strength;
};