JAVASCRIPT
Validate Password Strength (Min. 8 Chars, Mixed Case, Digit, Special)
Create a JavaScript function to validate password strength, enforcing requirements for minimum length, uppercase, lowercase, digits, and special characters.
function isStrongPassword(password) {
// Minimum 8 characters, at least one uppercase letter, one lowercase letter, one digit, and one special character
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+={}\[\]|:;"'<>,.?/~`-])[A-Za-z\d!@#$%^&*()_+={}\[\]|:;"'<>,.?/~`-]{8,}$/;
return passwordRegex.test(password);
}
// Examples:
// console.log(isStrongPassword("MyStrongP@ss1")); // true
// console.log(isStrongPassword("weakpass")); // false (no uppercase, no digit, no special)
// console.log(isStrongPassword("Password123")); // false (no special)
How it works: This JavaScript function validates password strength using a complex regex. It employs lookaheads `(?=...)` to ensure the password contains: at least one lowercase letter `(?=.*[a-z])`, one uppercase letter `(?=.*[A-Z])`, one digit `(?=.*\d)`, and one special character `(?=.*[!@#$%^&*...])`. Finally, it asserts a minimum length of 8 characters for the allowed character set `[A-Za-z\d!@#$%^&*...]{8,}$`.