Advertisement
EntropyStarRover

06. Password Validator

Jun 6th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function validatePassword(password) {
  2.     let isValid = true;
  3.     //check length
  4.     if (password.length < 6 || password.length > 10) {
  5.         console.log("Password must be between 6 and 10 characters");
  6.         isValid = false;
  7.     }
  8.  
  9.     //check special symbols
  10.     let format = /[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/;
  11.     let containsSpecial = false;
  12.     containsSpecial = format.test(password);
  13.  
  14.     if (containsSpecial) {
  15.         console.log("Password must consist only of letters and digits");
  16.         isValid = false;
  17.     }
  18.  
  19.     //check number of digits
  20.     let numbers = /[1234567890]/;
  21.     let numbersCount = 0;
  22.  
  23.     let arr = password.split("");
  24.     let containsNumber = false;
  25.  
  26.     //check if contains numbers
  27.     for (let symb of arr) {
  28.         containsNumber = numbers.test(symb);
  29.         if (containsNumber) {
  30.             numbersCount++;
  31.         }
  32.     }
  33.  
  34.     if (numbersCount < 2) {
  35.         isValid = false;
  36.         console.log("Password must have at least 2 digits");
  37.     }
  38.  
  39.  
  40.     if (isValid) {
  41.         console.log("Password is valid");
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement