Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _04_PasswordValidator
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. string password = Console.ReadLine();
  10. bool isValid = true;
  11.  
  12. isValid = PasswordLenght(password, isValid);
  13. isValid = OnlyLettersAndDigits(password, isValid);
  14. isValid = lastTwoCharecter(password, isValid);
  15. if (isValid)
  16. {
  17. Console.WriteLine("Password is valid");
  18. }
  19. }
  20.  
  21. static bool PasswordLenght(string input, bool isValid)
  22. {
  23.  
  24. if (input.Length < 6 || input.Length > 10)
  25. {
  26. isValid = false;
  27. Console.WriteLine("Password must be between 6 and 10 characters");
  28.  
  29. }
  30. return isValid;
  31. }
  32. static bool lastTwoCharecter(string input, bool isValid)
  33. {
  34. int inputLenght = input.Length;
  35. int lastCharecter = input[inputLenght - 1];
  36. int beforLastChar = input[inputLenght - 2];
  37. if (57 < lastCharecter || lastCharecter < 48 || 57 < beforLastChar || beforLastChar < 48)
  38. {
  39. isValid = false;
  40. Console.WriteLine("Password must have at least 2 digits");
  41. }
  42. return isValid;
  43. }
  44. static bool OnlyLettersAndDigits(string input, bool isValid)
  45. {
  46. bool isNotValid = false;
  47. for (int i = 0; i < input.Length; i++)
  48. {
  49. int number = input[i];
  50. if (number < 48 || 57 < number && number < 65 || 90 < number && number < 97 || 122 < number)
  51. {
  52. isNotValid = true;
  53. }
  54. }
  55. if (isNotValid)
  56. {
  57. isValid = false;
  58. Console.WriteLine("Password must consist only of letters and digits");
  59. }
  60. return isValid;
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement