I am trying to validate a password input field with minimum length of 8 characters and must contain at least 1 uppercase, 1 lowercase, 1 digit, and 1 special character. [If the password is valid, the label becomes green. If it's not valid the label becomes red.]
I think I got the minimum length part, but I'm not sure if I'm implementing my Javacsript correctly for the other requirements.
function green() {
var pw = document.getElementById('password').value;
var pattern = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$";
if (pw.value == pattern) {
document.getElementById('pw').style.color = "Green";
document.getElementById('pw').innerHTML = "Password: ✔";
} else {
document.getElementById('pw').style.color = "Black";
document.getElementById('pw').innerHTML = "Password:";
}
}
<label id="pw" for="password">Password:</label>
<input id="password" name="password" minlength="8" maxlength="50" class="form-control" type="password" onkeyup="green()" value="" />