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

106
Visualizações
How to check for special characters inside of a password using javascript

My code is setup to check for special characters inside of a given password. The program is suppose to store true to a variable if a special character was found, and false if one was not found; Afterwards, it will print the result of the variable. The problem is the program keeps printing out false even when there's a special character inside of the password. I don't know what's wrong

const arrayOfSp = ["!", "@", "#", "$", "%", "&", "*", "_", "-", "?"];
const password = "Patrick_";
let specialCharacterCheck = false;

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

for (i = 0; i < password.length; i++) {
  if (special(password[i])) {
    specialCharacterCheck = true;
  }
}
console.log(specialCharacterCheck);

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to break from the loop as soon as it finds a special character. To find special character in the array you can use find which will return undefined is no matching character is found

const arrayOfSp = ["!", "@", "#", "$", "%", "&", "*", "_", "-", "?"];
const password = "Patr_ick";
let specialCharacterCheck = false;

const special = (c) => {
  return arrayOfSp.find(item => item === c)
}

for (let i = 0; i < password.length; i++) {
  const isPresent = special(password[i]);
  if (isPresent) {
    specialCharacterCheck = true;
    break;
  }
}
console.log(specialCharacterCheck);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can simply use some and Set here

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

const specialChars = new Set(arrayOfSp);
let specialCharacterCheck = [...password].some((c) => specialChars.has(c));

console.log(specialCharacterCheck);

about 4 years ago · Juan Pablo Isaza Relatório

0

I like using a regex approach here:

var arrayOfSp = ["!", "@", "#", "$", "%", "&", "*", "_", "?", "-"];
var password = "Patrick_";
var regex = "[" + arrayOfSp.join("") + "]";
if (new RegExp(regex).test(password)) {
    console.log("Password is valid");
}
else {
    console.log("Password is invalid");
}

Note that I have deliberately placed - at the end of your input array, to avoid including an unwanted range of characters in the character class.

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