-
-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathpassword-validator.js
More file actions
43 lines (34 loc) · 1021 Bytes
/
password-validator.js
File metadata and controls
43 lines (34 loc) · 1021 Bytes
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
// A list of previously used passwords that cannot be reused.
const previousPasswords = ["Password1!", "Welcome2#", "Strong3$"];
function passwordValidator(password) {
// Ensure a password value is provided and that it is a string.
if (typeof password !== "string") {
return false;
}
// Password must be at least 5 characters long.
if (password.length < 5) {
return false;
}
// Password must contain at least one uppercase letter.
if (!/[A-Z]/.test(password)) {
return false;
}
// Password must contain at least one lowercase letter.
if (!/[a-z]/.test(password)) {
return false;
}
// Password must contain at least one number.
if (!/[0-9]/.test(password)) {
return false;
}
// Password must contain at least one allowed symbol.
if (!/[!#$%.*&]/.test(password)) {
return false;
}
// Password must not match any previous password.
if (previousPasswords.includes(password)) {
return false;
}
return true;
}
module.exports = passwordValidator;