JAVASCRIPT
Implement Robust Password Strength Validation
Validate password strength with JavaScript regex, ensuring it meets criteria for length, uppercase, lowercase, numbers, and special characters.
const validatePasswordStrength = (password) => {
const minLength = 8;
const hasUppercase = /[A-Z]/.test(password);
const hasLowercase = /[a-z]/.test(password);
const hasNumber = /[0-9]/.test(password);
const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password);
return password.length >= minLength && hasUppercase && hasLowercase && hasNumber && hasSpecialChar;
};
console.log(validatePasswordStrength("StrongP@ss1")); // true
console.log(validatePasswordStrength("weakpass")); // false
How it works: This JavaScript function `validatePasswordStrength` checks a password against multiple common security requirements using separate regular expressions. It verifies minimum length and the presence of uppercase letters, lowercase letters, numbers, and special characters. This modular approach allows for flexible and informative feedback to users during password creation.