Advertisement
ivo_petkov01

passwordValidator

Aug 21st, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function passwordValidator(input) {
  2.     let password = input;
  3.     let isValid = true;
  4.  
  5.     if (password.length < 6 || password.length > 10) {
  6.         isValid = false;
  7.         console.log('Password must be between 6 and 10 characters');
  8.     }
  9.  
  10.     for (let ch of password) {
  11.         if (!(ch.charCodeAt(0) >= 48 && ch.charCodeAt(0) <= 57) && (ch.charCodeAt(0) >= 65 && ch.charCodeAt(0) <= 90) && (ch.charCodeAt(0) >= 97 && ch.charCodeAt(0) <= 122)) {
  12.             isValid = false;
  13.             console.log('Password must consist only of letters and digits');
  14.             break;
  15.         }
  16.     }
  17.  
  18.     let digits = 0;
  19.     for (let i = 0; i < password.length; i++) {
  20.         let currentChar = password[i];
  21.         if (currentChar.charCodeAt(i) >= 48 && currentChar.charCodeAt(i) <= 57) {
  22.             digits++;
  23.             if (digits < 2) {
  24.                 isValid = false;
  25.                 console.log('Password must have at least 2 digits');
  26.             }
  27.         }
  28.     }
  29.  
  30.     if (isValid) {
  31.         console.log('Password is valid');
  32.     }
  33. }
  34. passwordValidator('MyPass123');
  35. passwordValidator('logIn');
  36. passwordValidator('Pa$s$s');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement