Advertisement
PowerCell46

Password Validator JS

Nov 21st, 2022
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function passwordValidator(password) {
  2.  
  3. passwordLengthCorrect = false;
  4. theDigitsAreFine = false;
  5. theNumberOfNumbersIsFine = false;
  6.  
  7. let currentDigitIsTrue = 0;
  8. let numberOfnumbers = 0;
  9.  
  10. if  (Number(password.length) >= 6 && Number(password.length <= 10)) {
  11. passwordLengthCorrect = true;
  12. } else {
  13. console.log("Password must be between 6 and 10 characters")
  14. }
  15.  
  16. for (let index = 0; index < Number(password.length); index++) { // the For cycle, responsible for the current digit
  17. let currentDigit = password[index];
  18.  
  19. for (let index1 = 48; index1 <= 57; index1++ ) { // the For cycle checking the numbers 0-9
  20.  
  21. if (currentDigit === String.fromCharCode(index1)) {
  22. numberOfnumbers++;
  23. currentDigitIsTrue++;
  24. }
  25. }
  26.  
  27. for (let index2 = 65; index2 <= 90; index2++) { // the For cycle checking the letters A-Z
  28.  
  29. if (currentDigit === String.fromCharCode(index2)) {
  30. currentDigitIsTrue++;
  31. }
  32. }
  33.  
  34. for (let index3 = 97; index3 <= 122; index3++) { // the For cycle checking the letters a-z
  35.  
  36. if (currentDigit === String.fromCharCode(index3)) {
  37. currentDigitIsTrue++;
  38. }
  39. }
  40.  
  41. }
  42.  
  43. if (currentDigitIsTrue === Number(password.length)) {
  44. theDigitsAreFine = true;
  45. } else {
  46. console.log("Password must consist only of letters and digits")
  47. }
  48.  
  49. if (numberOfnumbers < 2) {
  50. console.log("Password must have at least 2 digits")
  51. } else {
  52. theNumberOfNumbersIsFine = true;
  53. }
  54.  
  55. if (passwordLengthCorrect === true && theDigitsAreFine === true && theNumberOfNumbersIsFine === true) {
  56. console.log("Password is valid");
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement