-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy path04_solve.js
36 lines (30 loc) · 1.15 KB
/
04_solve.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
let password = "Aditya@2005";
const passwordValidator = (password) => {
const regex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[\W_])[A-Za-z\d\W_]{8,}$/;
// Regex Breakdown
// ^ → Start of the string
// (?=.*[A-Z]) → At least one uppercase letter
// (?=.*[a-z]) → At least one lowercase letter
// (?=.*\d) → At least one digit
// (?=.*[\W_]) → At least one special character (\W matches any non-word character, _ is included explicitly)
// [A-Za-z\d\W_]{8,} → Matches atleast 8 characters (letters, numbers, and special characters)
// $ → End of the string
return regex.test(password);
};
//Alternate
function isValidString(str) {
if (str.length < 8) return false;
let hasUpper = false,
hasLower = false,
hasDigit = false,
hasSpecial = false;
for (let char of str) {
if (/[A-Z]/.test(char)) hasUpper = true;
if (/[a-z]/.test(char)) hasLower = true;
if (/\d/.test(char)) hasDigit = true;
if (/[\W_]/.test(char)) hasSpecial = true;
}
return hasUpper && hasLower && hasDigit && hasSpecial;
}
console.log(passwordValidator(password));
console.log(isValidString(password));