function checkPasswordStrength(password) {
const criteria = {
length: false,
uppercase: false,
lowercase: false,
number: false,
specialChar: false,
};
if (password.length >= 8) {
criteria.length = true;
}
if (/[A-Z]/.test(password)) {
criteria.uppercase = true;
}
if (/[a-z]/.test(password)) {
criteria.lowercase = true;
}
if (/\d/.test(password)) {
criteria.number = true;
}
if (/[$@!%*?&]/.test(password)) {
criteria.specialChar = true;
}
const numCriteriaMet = Object.values(criteria).filter(Boolean).length;
switch (numCriteriaMet) {
case 1:
return 'Weak';
case 2:
return 'Moderate';
case 3:
return 'Strong';
case 4:
return 'Very strong';
case 5:
return 'Extremely strong';
default:
return 'Invalid password';
}
}