Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
How to check for special characters in password

I am trying to validate a given password by checking for uppercase, lowercase, and special characters. The program is suppose to store true for each requirement found and false for each not found. If anyone of the requirements are not found then the program prints an error message as well as the finding results for each requirement. The problem is the special character variable keeps returning as false even when there's a special character in the password. It seems that the function special that calls for the special character to get checked never gets called but I don't know why. Can anybody help?

// Assume password input is "Patick_"
const passwordForSignup = document.getElementById("input-password");

const arrayOfSp = ["!", "@", "#", "$", "%", "&", "*", "_", "-", "?"];

const special = (c) => {
    console.log(c);
    for (let i = 0; i < arrayOfSp.length; i++)
    {
        if (c === arrayOfSp[i])
        {
            return true;
        }
    }
    return false;
}

if (passwordForSignup.value.length >= 6 && passwordForSignup.value.length <= 17)
{
    let upperCaseCheck = false;
    let lowerCaseCheck = false;
    let specialCharacterCheck = false;
    let passwordValue = "";
            
    for (let i = 0; i < passwordForSignup.value.length; i++)
    {
        passwordValue = passwordForSignup.value[i];

        if (passwordForSignup.value[i] === passwordValue.toUpperCase())
        {
            upperCaseCheck = true;
        }
        else if (passwordForSignup.value[i] === passwordValue.toLowerCase())
        {
            lowerCaseCheck = true;
        }
        else if (special(passwordValue))
        {
            specialCharacterCheck = true;
        }
    }

    if (!upperCaseCheck || !lowerCaseCheck || !specialCharacterCheck)
    {
        console.log("Something is wrong");
        console.log(upperCaseCheck);
        console.log(lowerCaseCheck);
        console.log(specialCharacterCheck);
    }
}

// else 
// {
//     message.push("Password must contain 6 - 17 characters");
//     errorFound(message, e);
// }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The rason is that your special check is made for last and

"_".toUpperCase()

for example return "_"
Try:

        if (special(passwordValue))
        {
            specialCharacterCheck = true;
        }
        else if (passwordForSignup.value[i] === passwordValue.toUpperCase())
        {
            upperCaseCheck = true;
        }
        else if (passwordForSignup.value[i] === passwordValue.toLowerCase())
        {
            lowerCaseCheck = true;
        }
     

However this isn't the way to do. Instead use regular expressions.

about 4 years ago · Juan Pablo Isaza Relatório

0

Try the Regular Expression patterns, like this example.

const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[-+_!@#$%^&*., ?]).+$/;

function check_pass(){
  var pass=document.getElementsByName("pass")[0].value;
  
  console.log(regex.test(pass));
}
<input type='text' name='pass'><button id='test' onclick='check_pass()'>Check</button>

where:

^ represents the starting of the string.

(?=.*[a-z]) represent at least one lowercase character.

(?=.*[A-Z]) represents at least one uppercase character.

(?=.*\d) represents at least one numeric value.

(?=.*[-+_!@#$%^&*., ?]) represents at least one of the special character.

And if you want separeted checks, can be made like this:

const lower = /^(?=.*[a-z])/;
const upper = /^(?=.*[A-Z])/;
const nums = /^(?=.*\d)/;
const special = /^(?=.*[-+_!@#$%^&*., ?]).+$/;


function check_pass() {
  var pass = document.getElementsByName("pass")[0].value;

  console.log('Lower: ' + lower.test(pass));
  console.log('Upper: ' + upper.test(pass));
  console.log('Numbers: ' + nums.test(pass));
  console.log('Specials: ' + special.test(pass));

}
<input type='text' name='pass'><button id='test' onclick='check_pass()'>Check</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda