Advertisement
Neri0817

06. Password Validator

Jan 31st, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function validator(password) {
  2.   let letters = /^[a-zA-Z]+$/;
  3.   let numbers = /^[0-9]+$/;
  4.   let symbols = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;
  5.   let charCounter = 0;
  6.   let numCounter = 0;
  7.   let symbolsCounter = 0;
  8.   for (let i = 0; i < password.length; i++) {
  9.     let currentEl = password.split("");
  10.     if (currentEl[i].match(letters)) {
  11.       charCounter++;
  12.     } else if (currentEl[i].match(numbers)) {
  13.       numCounter++;
  14.     }
  15.  
  16.     if (currentEl[i].match(symbols)) {
  17.       symbolsCounter++;
  18.     }
  19.   }
  20.  
  21.   if (charCounter >= 6 && charCounter <= 10 && numCounter >= 2) {
  22.     console.log("Password is valid");
  23.   }
  24.  
  25.   if (symbolsCounter > 0) {
  26.     console.log("Password must consist only of letters and digits");
  27.     console.log("Password must have at least 2 digits");
  28.   } else if (charCounter < 6 || (charCounter > 10 && numCounter == 0)) {
  29.     console.log("Password must be between 6 and 10 characters");
  30.     console.log("Password must have at least 2 digits");
  31.   }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement