I'm trying to meet requirements from a client with a "New Password" solution.
I have this requirements:
1 - New password must be 8 - 13 characters,
2 - password must contain numbers,
3 - password must contain uppercase and lowercase,
4 - password must not contain username and finally
5 - password must not be used before.
I'm not an expert in Javascript and I have been trying to put all this requirements in a script, but i'm not sure how to solve the part 1,4,5 of this requirement, so i'm hoping someone can help me with what could I do in order to achieve all those requirements...
Also, my "Weak Password , Medium Password, Strong Password" message it's being showed at the top of a input element who was a ID called msg (just so you know)
Here is my script:
function validatePassword(password) {
// Do not show anything when the length of password is zero.
if (password.length === 0) {
document.getElementById("msg").innerHTML = "";
return;
}
// Create an array and push all possible values that you want in password
var matchedCase = new Array();
matchedCase.push("[$@$!%*#?&]"); // Special Charector
matchedCase.push("[A-Z]"); // Uppercase Alpabates
matchedCase.push("[0-9]"); // Numbers
matchedCase.push("[a-z]"); // Lowercase Alphabates
// Check the conditions
var ctr = 0;
for (var i = 0; i < matchedCase.length; i++) {
if (new RegExp(matchedCase[i]).test(password)) {
ctr++;
}
}
// Display it
var color = "";
var strength = "";
switch (ctr) {
case 0:
case 1:
case 2:
strength = "Contraseña Débil";
color = "red";
break;
case 3:
strength = "Contraseña Regular";
color = "orange";
break;
case 4:
strength = "Contraseña Fuerte";
color = "green";
break;
}
document.getElementById("msg").innerHTML = strength;
document.getElementById("msg").style.color = color;
}