I have written a program to validate password input based on different criteria and if those criteria are met, 25% is added at each point and the total(passwordStrength) is output at the end. This is the implementation:
let passwordStrength = 0;
function checkPassword(password) {
if (typeof password !== 'string' || password.trim() === ''){
return 'Only strings are allowed';
}
let trimmedPassword = password.trim();
let regex1 = /[a-z]/;
let regex2 = /[A-Z]/;
let regex3 = /[0-9]/;
let regex4 = /[$@#&!]/;
if (regex1.test(trimmedPassword)){
passwordStrength += 25;
}
if (regex2.test(trimmedPassword)){
passwordStrength += 25;
}
if (regex3.test(trimmedPassword)){
passwordStrength += 25;
}
if (regex4.test(trimmedPassword)){
passwordStrength += 25;
}
if (trimmedPassword.length < 6 || trimmedPassword.length > 12){
return 'Password can not be less than 6 or greater than 12 characters';
}
return passwordStrength += '%';
}
console.log(checkPassword('@manGaLa'));// returns 75%
Now, I want to add the respective statements accompanying the various strengths like so:
if passwordStrength === 25%, return poor,
if passwordStrength === 50%; return weak,
if passwordStrength === 75%; return medium,
if passwordStrength === 100%; return strong.
How do I add this without making the programme too long?