Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!