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);
// }
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.
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>