JAVASCRIPT
Validating Password Strength with Multiple Regex Criteria
Enforce strong passwords by combining multiple regular expressions to check for minimum length, uppercase, lowercase, numbers, and special characters.
function checkPasswordStrength(password) {
const minLength = 8;
const hasUpperCase = /[A-Z]/;
const hasLowerCase = /[a-z]/;
const hasNumber = /[0-9]/;
const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/;
let strength = {
length: password.length >= minLength,
upper: hasUpperCase.test(password),
lower: hasLowerCase.test(password),
number: hasNumber.test(password),
special: hasSpecialChar.test(password)
};
return {
isValid: Object.values(strength).every(Boolean),
criteria: strength
};
}
// Examples:
console.log(checkPasswordStrength("Passw0rd!"));
// { isValid: true, criteria: { length: true, upper: true, lower: true, number: true, special: true } }
console.log(checkPasswordStrength("weakpass"));
// { isValid: false, criteria: { length: true, upper: false, lower: true, number: false, special: false } }
How it works: This snippet checks a password's strength against multiple criteria: minimum length, presence of uppercase letters, lowercase letters, numbers, and special characters. Each criterion is validated using a separate regular expression. The function returns an object indicating overall validity and the status of each individual requirement, useful for providing specific feedback to users.