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

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

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 Report

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 Report

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 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!