Spocoman

06. Password Validator

Jan 28th, 2022 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (pass) {
  2.  
  3.     let digit = 0;
  4.     let isValid = true;
  5.  
  6.     if (pass.length < 6 || pass.length > 10) {
  7.         console.log('Password must be between 6 and 10 characters');
  8.         isValid = false;
  9.     }
  10.  
  11.     for (let i = 0; i < pass.length; i++) {
  12.         if (pass[i] >= 'A' && pass[i] <= 'Z' || pass[i] >= 'a' && pass[i] <= 'z' || pass[i] >= '0' && pass[i] <= '9') {
  13.             if (pass[i].charCodeAt(0) > 47 && pass[i].charCodeAt(0) < 58) {
  14.                 digit++;
  15.             }
  16.         } else {
  17.             console.log('Password must consist only of letters and digits');
  18.             isValid = false;
  19.             break;
  20.         }
  21.     }
  22.  
  23.     if (digit < 2) {
  24.         console.log('Password must have at least 2 digits');
  25.         isValid = false;
  26.     }
  27.  
  28.     if (isValid) {
  29.         console.log('Password is valid');
  30.     }
  31. }
  32.  
Add Comment
Please, Sign In to add comment