JAVASCRIPT
Validating Strong Passwords with Multiple Criteria
Implement robust password validation using a single regex that checks for minimum length, uppercase, lowercase, numbers, and special characters.
function isStrongPassword(password) {
// Must contain at least one uppercase letter, one lowercase letter, one number,
// one special character, and be at least 8 characters long.
const strongPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+={}\[\]|\\:;"'<>,.?/~`-])[A-Za-z\d!@#$%^&*()_+={}\[\]|\\:;"'<>,.?/~`-]{8,}$/;
return strongPasswordRegex.test(password);
}
// Examples:
// console.log(isStrongPassword('Passw0rd!')); // true
// console.log(isStrongPassword('weakpassword')); // false (no uppercase, number, special)
// console.log(isStrongPassword('Password123')); // false (no special char)
// console.log(isStrongPassword('P@ss1')); // false (too short)
How it works: This JavaScript function checks if a given password meets 'strong' criteria using a single regular expression. It employs lookaheads (`(?=...)`) to assert the presence of at least one lowercase letter, one uppercase letter, one digit, and one special character from a defined set, all while ensuring the password is at least 8 characters long.