TZinovieva

Password Validator JS

Feb 4th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function passwordValidator(password) {
  2.     let valLength = validLength(password);
  3.     let valSymbols = checkValidSymbols(password);
  4.     let valDigits = checkTwoDigits(password);
  5.     printResult(valLength, valSymbols, valDigits);
  6.  
  7.     function validLength(password) {
  8.         return password.length >= 6 && password.length <= 10;
  9.     }
  10.     function checkValidSymbols(text) {
  11.         for (let sym of text) {
  12.             let symbol = sym.charCodeAt(0);
  13.  
  14.             let digitsInPassword = checkForDigits(symbol);
  15.             let letters = checkForLetters(symbol);
  16.             if (!digitsInPassword && !letters) {
  17.                 return false;
  18.             }
  19.         }
  20.         return true;
  21.     }
  22.  
  23.     function checkForDigits(char) {
  24.         return char >= 48 && char <= 57
  25.     }
  26.  
  27.     function checkForLetters(char) {
  28.         return (char >= 65 && char <= 90) || (char >= 97 && char <= 122)
  29.     }
  30.  
  31.     function checkTwoDigits(password) {
  32.         let digitCounter = 0;
  33.         for (let sym of password) {
  34.             let digitsInPassword = checkForDigits(sym.charCodeAt(0));
  35.             if (digitsInPassword) {
  36.                 digitCounter++;
  37.             }
  38.         }
  39.         return digitCounter >= 2
  40.     }
  41.  
  42.     function printResult(valLength, valSymbols, valDigits) {
  43.         if (!valLength) {
  44.             console.log("Password must be between 6 and 10 characters");
  45.         }
  46.         if (!valSymbols) {
  47.             console.log("Password must consist only of letters and digits");
  48.         }
  49.         if (!valDigits) {
  50.             console.log("Password must have at least 2 digits");
  51.         }
  52.         if (valLength && valSymbols && valDigits) {
  53.             console.log("Password is valid");
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment