JAVASCRIPT
Validate Password Strength
Implement a JavaScript function to validate password strength, ensuring it includes uppercase, lowercase, numbers, special characters, and meets a minimum length.
function isStrongPassword(password) {
// Minimum 8 characters, at least one uppercase, one lowercase, one number, one special character
const strongPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
return strongPasswordRegex.test(password);
}
// Examples
console.log(isStrongPassword("Password123!")); // true
console.log(isStrongPassword("weakpass")); // false (no uppercase, number, special)
console.log(isStrongPassword("StrongPwd")); // false (no number, special)
How it works: This snippet provides a JavaScript function `isStrongPassword` that uses a regular expression to enforce password strength rules. It checks for a minimum length of 8 characters and the presence of at least one uppercase letter, one lowercase letter, one digit, and one common special character, ensuring robust security.