JAVASCRIPT
Enforce Password Strength
Implement a JavaScript function to validate password strength, requiring minimum length, uppercase, lowercase, numbers, and special characters.
function isStrongPassword(password) {
// Minimum 8 characters, at least one uppercase letter, one lowercase letter, one number, and one special character
const strongRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\|,.<>/?]).{8,}$/;
return strongRegex.test(password);
}
console.log(isStrongPassword("P@ssword123")); // true
console.log(isStrongPassword("password")); // false (no uppercase, no special char, no number)
How it works: The `isStrongPassword` function checks if a password meets specific strength criteria using a lookahead-based regular expression. It ensures the password is at least 8 characters long and contains at least one lowercase letter, one uppercase letter, one digit, and one special character. This is crucial for enhancing application security by enforcing good password practices during user registration or password changes.