I have a problem with my code where only the first "if" block get evaluated, but the next two "if" blocks does not get evaluated. The last two "if" blocks get evaluated only if the first "if" block get evaluated.
Basically, My code here checks the value of the input field via onInput (which is a password) so that it matches the following requirements...
- ONE uppercase letter.
- ONE lowercase letter.
- ONE special character.
- Numbers.
...with these regex that I've come up with:/[A-Z]/g,/[a-z]/g,/\W|_/g
Can anyone explain why is the last two "if" blocks does not get evaluated if the first "if" block is false?
EDIT: I was trying to create a code where if the user input their password that doesn't match the requirements, the input field will change color to red and stay red until the user fixed his/her password and fulfill the requirement. But when I executed the code and try to key in jj, the input field does not turn red until I input jjH , which is not what I was expecting.
<input type="password" id="password" placeholder="Password..." maxlength="6" onInput="verifyPasswordFormat(value, 'password')">
function verifyPasswordFormat(password, id){
var elem_Id = document.getElementById(id);
var upperCase = /[A-Z]/g;
var lowerCase = /[a-z]/g;
var specialChar = /\W|_/g;
if(password != ""){
if(password.match(upperCase).length > 1){
elem_Id.style.backgroundColor = "#E9C7C7";
elem_Id.style.borderColor = "#CB2424";
}else if(password.match(lowerCase).length > 1){
elem_Id.style.backgroundColor = "#E9C7C7";
elem_Id.style.borderColor = "#CB2424";
}else if(password.match(specialChar).length > 1){
elem_Id.style.backgroundColor = "#E9C7C7";
elem_Id.style.borderColor = "#CB2424";
}else{
elem_Id.style.backgroundColor = "white";
elem_Id.style.borderColor = "#BCBCA8";
}
}
function verifyPasswordFormat(password) {
var upperCase = /[A-Z]/g;
var lowerCase = /[a-z]/g;
var specialChar = /\W|_/g;
if (password != "") {
if (password.match(upperCase).length > 1) {
console.log('first');
}
if (password.match(lowerCase).length > 1) {
console.log('second');
}
if (password.match(specialChar).length > 1) {
console.log('third');
}
}
}
verifyPasswordFormat('jjHH@!');
Aucun commentaire:
Enregistrer un commentaire