tsvetelinapasheva

PasswordValidator/MethodsExericise

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