yanchevilian

04. PasswordValidator

Feb 13th, 2021
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _4._Password_Validator
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string password = Console.ReadLine();
  10.             CheckIfPasswordIsValid(password);
  11.         }
  12.  
  13.         private static void CheckIfPasswordIsValid(string password)
  14.         {
  15.             bool isPasswordInRange = CheckIfPasswordIsInRange(password);
  16.             bool isPasswordConsistOnlyLetterAndDigits = CheckPasswordConsist(password);
  17.             bool isPasswordHaveAtLeast2Digits = CheckIfPasswordHaveDigits(password);
  18.             if (isPasswordInRange == true && isPasswordConsistOnlyLetterAndDigits == true && isPasswordHaveAtLeast2Digits == true)
  19.             {
  20.                 Console.WriteLine("Password is valid");
  21.             }
  22.             if (isPasswordInRange == false)
  23.             {
  24.                 Console.WriteLine("Password must be between 6 and 10 characters");
  25.             }
  26.             if (isPasswordConsistOnlyLetterAndDigits == false)
  27.             {
  28.                 Console.WriteLine("Password must consist only of letters and digits");
  29.             }
  30.             if (isPasswordHaveAtLeast2Digits == false)
  31.             {
  32.                 Console.WriteLine("Password must have at least 2 digits");
  33.             }
  34.         }
  35.         private static bool CheckIfPasswordHaveDigits(string password)
  36.         {
  37.             bool isItTrue = false;
  38.             int digitsCounter = 0;
  39.             foreach (var digit in password)
  40.             {
  41.                 if (char.IsDigit(digit))
  42.                 {
  43.                     digitsCounter++;
  44.                 }
  45.             }
  46.             if (digitsCounter >= 2)
  47.             {
  48.                 isItTrue = true;
  49.             }
  50.             return isItTrue;
  51.         }
  52.  
  53.         private static bool CheckPasswordConsist(string password)
  54.         {
  55.             bool isPasswordConsistLetterAndDigits = false;
  56.             foreach (var currentItems in password)
  57.             {
  58.                 if (char.IsLetterOrDigit(currentItems))
  59.                 {
  60.                     isPasswordConsistLetterAndDigits = true;
  61.                 }
  62.                 else
  63.                 {
  64.                     isPasswordConsistLetterAndDigits = false;
  65.                     break;
  66.                 }
  67.             }
  68.             return isPasswordConsistLetterAndDigits;
  69.         }
  70.  
  71.         private static bool CheckIfPasswordIsInRange(string input)
  72.         {
  73.             bool isInRange = false;
  74.             if (input.Length >= 6 && input.Length <= 10)
  75.             {
  76.                 isInRange = true;
  77.             }
  78.             return isInRange;
  79.         }
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment