JAVASCRIPT
Validate Password Strength (Complex)
Check password strength using a JavaScript regex to enforce minimum length and the inclusion of uppercase, lowercase, numbers, and special characters for enhanced security.
function isStrongPassword(password) {
const strongRegex = new RegExp(
"^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})"
);
return strongRegex.test(password);
}
// Example usage:
console.log(isStrongPassword("MyStrongP@ss1")); // true
console.log(isStrongPassword("weakpass")); // false (no uppercase, number, special char)
console.log(isStrongPassword("Short@1")); // false (too short)
How it works: This snippet provides a JavaScript function to validate password strength using a regular expression with multiple positive lookaheads. It ensures the password contains at least one lowercase letter (`(?=.*[a-z])`), one uppercase letter (`(?=.*[A-Z])`), one digit (`(?=.*[0-9])`), one special character (`(?=.*[!@#$%^&*])`), and has a minimum length of 8 characters (`(?=.{8,})`). This helps enforce better security practices for user passwords.