javascript – Check password input

Question:

Hello, I'm starting to learn JavaScript and I'm not getting in any way to make my code validate if there are 3 uppercase characters, 2 numbers and 1 special character in my input. I would like to know what I should be doing wrong, because I made several attempts, searched on several sites and no way works. Here's the base code:

            /* Validação do Campo Senha*/
        if (document.formulario.senha.value.length < 8) {
           alert("A senha deve conter no minímo 8 digitos!");
           document.formulario.senha.focus();
           return false;
       }

Thanks for listening!

Answer:

Larissa , use REGEX:

var senha = document.formulario.senha;
var regex = /^(?=(?:.*?[A-Z]){3})(?=(?:.*?[0-9]){2})(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%;*(){}_+^&]*$/; 

if(senha.length < 8)
{
    alert("A senha deve conter no minímo 8 digitos!");
    document.formulario.senha.focus();
    return false;
}
else if(!regex.exec(senha))
{
    alert("A senha deve conter no mínimo 3 caracteres em maiúsculo, 2 números e 1 caractere especial!");
    document.formulario.senha.focus();
    return false;
}
return true;

Explaining regex:

// (?=(?:.*?[A-Z]){3}) - Mínimo 3 letras maiúsculas
// (?=(?:.*?[0-9]){2}) - Mínimo 2 números
// (?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#;$%*(){}_+^&] - Mínimo 1 caractere especial
Scroll to Top