AlexzanderF

Password Validator

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