Advertisement
vladovip

Password Validator - JS Fundamentals_Ex

Feb 5th, 2022
2,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function passwordValidator(stringPassword) {
  2.   function isLengthEnough(stringPassword) {
  3.     return stringPassword.length >= 6 && stringPassword.length <= 10;
  4.   }
  5.  
  6.   function isLetterAndDigits(stringPassword) {
  7.     for (let currentCharIndex of stringPassword) {
  8.       let currentCharCode = currentCharIndex.charCodeAt(0);
  9.       if (
  10.         !(currentCharCode >= 65 && currentCharCode <= 90) &&
  11.         !(currentCharCode >= 97 && currentCharCode <= 122) &&
  12.         !(currentCharCode >= 48 && currentCharCode <= 57)
  13.       ) {
  14.         return false;
  15.       }
  16.     }
  17.     return true;
  18.   }
  19.   // console.log(isLetterAndDigits('logIn'));
  20.  
  21.   function isHavingTwoNumbers(stringPassword) {
  22.     let counter = 0;
  23.     for (let currentCharIndex of stringPassword) {
  24.       let currentCharCode = currentCharIndex.charCodeAt(0);
  25.       if (currentCharCode >= 48 && currentCharCode <= 57) {
  26.         counter++;
  27.       }
  28.     }
  29.     return counter >= 2 ? true : false;
  30.   }
  31.   // console.log(isHavingTwoNumber('logIn'));
  32.  
  33.   let isLengthValid = isLengthEnough(stringPassword);
  34.   let isLettersAndDigitsOnly = isLetterAndDigits(stringPassword);
  35.   let isTwoNumbersAtLeast = isHavingTwoNumbers(stringPassword);
  36.  
  37.   if (isLengthValid && isLettersAndDigitsOnly && isTwoNumbersAtLeast) {
  38.     console.log("Password is valid");
  39.   }
  40.   if (!isLengthValid) {
  41.     console.log("Password must be between 6 and 10 characters");
  42.   }
  43.   if (!isLettersAndDigitsOnly) {
  44.     console.log("Password must consist only of letters and digits");
  45.   }
  46.   if (!isTwoNumbersAtLeast) {
  47.     console.log("Password must have at least 2 digits");
  48.   }
  49. }
  50.  
  51. passwordValidator("logIn");
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement