Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda