gospod1978

Methods-Ex\Password Validator

Oct 13th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using System;
  2.  
  3. namespace methods_exerci
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.  
  10.             string password = Console.ReadLine();
  11.             bool isValid = true;
  12.  
  13.             isValid = PasswordLength(password, isValid);
  14.             isValid = IsLetterOrDigit(password, isValid);
  15.             isValid = IsDigit(password, isValid);
  16.            
  17.             PasswordIsValid(isValid);
  18.  
  19.         }
  20.  
  21.         static bool PasswordLength(string password, bool isValid)
  22.         {
  23.             if (password.Length < 6 || password.Length > 10)
  24.             {
  25.                 Console.WriteLine("Password must be between 6 and 10 characters");
  26.                 isValid = false;
  27.             }
  28.             return isValid;
  29.         }
  30.  
  31.  
  32.  
  33.         static bool IsLetterOrDigit(string password, bool isValid)
  34.         {
  35.             for (int i = 0; i < password.Length; i++)
  36.             {
  37.                 if (!Char.IsLetterOrDigit(password[i]))
  38.                 {
  39.                     Console.WriteLine("Password must consist only of letters and digits");
  40.                     isValid = false;
  41.                     break;
  42.                 }
  43.             }
  44.             return isValid;
  45.         }
  46.  
  47.         static bool IsDigit(string password, bool isValid)
  48.         {
  49.             int digit = 0;
  50.  
  51.             for (int i = 0; i < password.Length; i++)
  52.             {
  53.                 if (Char.IsDigit(password[i]))
  54.                 {
  55.                     digit++;
  56.                 }
  57.             }
  58.  
  59.             if (digit < 2)
  60.             {
  61.                 Console.WriteLine("Password must have at least 2 digits");
  62.                 isValid = false;
  63.             }
  64.             return isValid;
  65.         }
  66.  
  67.         static void PasswordIsValid(bool isValid)
  68.         {
  69.             if (isValid)
  70.             {
  71.                 Console.WriteLine("Password is valid");
  72.             }
  73.         }
  74.     }
  75. }
Add Comment
Please, Sign In to add comment