-2

I am trying to create a function where it verifies the strength of a password. I've seen a few examples online for other langauges but not for javascript.

  • password cannot be null
  • 8 characters
  • 1 uppercase letter
  • 1 lowercase letter
  • 1 number

I've noticed some people using Regex but is there any other way for this? I've tried the below which gets me up to verifying the caps letters but doesnt verify lowercase or numbers.

const passwordVerifier = (password) => {

let result = ''
let upperCase = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", 
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
let lowerCase = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", 
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

//The password should not be null
if(password !== null){
  //The password should be larger than 8 chars
  if(password.length >= 8 ){   
    //The password should have one uppercase/ lowercase letter
    for(let i = 0; i <= password.length; i++){
      if(upperCase.indexOf(password[i]) >= 1 && lowerCase.indexOf(password[i]) >= 1){
        result += 'Strong Password'
        return result
      }
        
    } result += 'Must have caps and lowcaps'
        return result
        
  } result += 'Must have 8 characters'
        return result
 }


 result += 'Password cannot be null'
 return result
 }


 console.log(passwordVerifier("Password123"))

Also how can I get it to check if it has numbers?

mmiah
  • 65
  • 7
  • Does this answer your question? [Password REGEX with min 6 chars, at least one letter and one number and may contain special characters](https://stackoverflow.com/questions/7844359/password-regex-with-min-6-chars-at-least-one-letter-and-one-number-and-may-cont) – sinanspd Oct 21 '21 at 23:31
  • The code is attempting to do a lowercase check - have you done any debugging to find out why that bit is not working? What example password are you using? – halfer Oct 21 '21 at 23:31
  • @halfer console.log(passwordVerifier("Password123")) is the example. I've tried debugging the code but cant seem to get it working – mmiah Oct 21 '21 at 23:44
  • Ah, the algorithm is wrong - for each character in the password, you are expecting every character to be both lower-case and upper-case. That won't work - maybe a better algorithm would be to count the lower/upper case letters in a loop and then run conditionals after the loop ends? – halfer Oct 22 '21 at 07:53
  • @halfer I mean thats why I came to stackoverflow to find a way to create a better algorithm.. – mmiah Oct 22 '21 at 11:17

2 Answers2

1

Here is a simple validation

with regex

const validatePassword=(password)=> {
  
  if (!password)
      return "Cant be empty";
  if (password.length<8)
      return "Can't be lower than 8 character in length";
  if (!/[A-Z]/g.test(password))
      return "At least one upper character is needed";
      
   if (!/[a-z]/g.test(password))
      return "At least one lower character is needed";
   
  if (!/[0-9]/g.test(password))
      return "At least one number is needed";
      
    return true;    

}

// false
console.log(validatePassword("tests01a")) 

// true
console.log(validatePassword("testS01a")) 

Without regex

let upperCase = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", 
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
const validatePassword=(password)=> {
      if (!password)
          return "Cant be empty";
      if (password.length<8)
          return "Can't be lower than 8 character in length";
      if (!password.split("").find(x=> upperCase.find(a=> a == x)))
          return "At least one upper character is needed";
          
       if (!password.split("").find(x=> upperCase.find(a=> a.toLowerCase() == x)))
          return "At least one lower character is needed";
       
      if (!password.split("").find(x=> !isNaN(parseInt(x))))
          return "At least one number is needed";
          
        return true;    

    }

    // false
    console.log(validatePassword("tests01a")) 

    // true
    console.log(validatePassword("testS01a")) 
Alen.Toma
  • 4,684
  • 2
  • 14
  • 31
  • I know OP suggested to use ways other than regex, but really, this is the best approach if you don't have that constraint. At least it's not using *only* regex. – hayavuk Oct 22 '21 at 00:34
  • Obs I have missed that, I did a version without regex to so the OP should be happy now. – Alen.Toma Oct 22 '21 at 01:22
0

This can be solved in a simpler way without regexes by doing it character by character. Something like this:

const UPPERCASE = new Set('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
const LOWERCASE = new Set('abcdefghijklmnopqrstuvwxyz')
function isNumber(c) { return c === '' + Number(c) }

function checkPw(s) {
  // First eliminate cases that are easy to flag
  if (s == null) return false
  if (s.length < 8) return false

  let foundUppercase = false
  let foundLowercase = false
  let foundNumber = false

  for (let c of s) {
    foundUppercase = foundUppercase || UPPERCASE.has(c)
    foundLowercase = foundLowercase || LOWERCASE.has(c)
    foundNumber = foundNumber || isNumber(c)
    if (foundUppercase && foundLowercase && foundNumber) return true
  }

  return false
}
hayavuk
  • 351
  • 2
  • 6
  • Why is the isNumber and checkPw two different functions? Kind of defeats the purpose of a single password verification function? Unless I'm misunderstanding this – mmiah Oct 22 '21 at 00:44
  • It's mostly for clarity. You could inline the comparison if that really bothers you. Though, I don't think the function count as a requirement adds any value to your program. – hayavuk Oct 22 '21 at 22:56