Liliana797979

password validator fundamentals

May 14th, 2021
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function passValidator(password){
  3.     let enoughLength = false;
  4.     let onlyLetterAndDigits = true;
  5.     let digits = 0;
  6.     if(password.length >= 6 && password.length <= 10){
  7.         enoughLength = true;
  8.     }
  9.     for(let i = 0; i < password.length; i++){
  10.         if(password[i].charCodeAt(0) >= 48 && password[i].charCodeAt(0) <= 57){    // digits
  11.             digits++;
  12.         } else if((password[i].charCodeAt(0) >= 65 && password[i].charCodeAt(0) <= 90) || (password[i].charCodeAt(0) >= 97 && password[i].charCodeAt(0) <= 122)){    // letters
  13.             continue;
  14.         } else {
  15.             onlyLetterAndDigits = false;
  16.         }
  17.  
  18.     }
  19.     if(enoughLength === true && digits >= 2 && onlyLetterAndDigits === true){
  20.         console.log("Password is valid");
  21.     } else{
  22.         if(!enoughLength){
  23.             console.log("Password must be between 6 and 10 characters");
  24.         }
  25.         if(!onlyLetterAndDigits > 0){
  26.             console.log("Password must consist only of letters and digits");
  27.         }
  28.         if(digits < 2){
  29.             console.log("Password must have at least 2 digits");
  30.         }    
  31.     }
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment