Home > database >  How to check for special characters inside of a password using javascript
How to check for special characters inside of a password using javascript

Time:12-13

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);

CodePudding user response:

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);

CodePudding user response:

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.

CodePudding user response:

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);

CodePudding user response:

I'd use a functional approach:

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

function hasSpecial (specials, password) {
  for (const str of specials) {
    if (!password.includes(str)) continue;
    return true;
  }
  return false;
}

console.log(hasSpecial(arrayOfSp, password));

CodePudding user response:

You can also use String.prototype.includes() to check if the string contains any specific character or string.

function doesContainSpecial(string) {
  const specialChars = ["!", "@", "#", "$", "%", "&", "*", "_", "-", "?"];
  for (let i = 0; i < string.length; i  ) {
    if (string.includes(specialChars[i])) return true;
  }

  return false;
}

console.log(doesContainSpecial("Patri_ck")); // Expected Output: True
console.log(doesContainSpecial("Patrick")); // Expected Output: False

You can read more about String.prototype.includes() at MDN Docs

You can also use Regular Expression To Do The Same Task:

function doesContainSpecial(string) {
  var specialChars = "[!@#$%&*_?-]" // Put all your Special Characters Between Square Brackets
  var regex = new RegExp(specialChars)
  return regex.test(string)
}

console.log(doesContainSpecial("Patri_ck")); // Expected Output: True
console.log(doesContainSpecial("Patrick")); // Expected Output: False

Using Regex is much more efficient than using .includes()

You can read more about Regular Expressions at MDN Docs

CodePudding user response:

Short answer is:

[...password].some((e) => arrayOfSp.includes(e))

But if you need validate object (including password) may be it worth use a special tool for validation like yup.

  • Related