anizko

04. Password Validator

Jul 2nd, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _04._Password_Validator
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string password = Console.ReadLine();
  10.             bool isValidPassword = true;
  11.  
  12.             bool isValidChekPasswordLength= ChekPasswordLength(password, isValidPassword);
  13.             bool isValidChekPasswordConsistLettersDigits= ChekPasswordConsistLettersDigits(password, isValidPassword);
  14.             bool isValidChekPasswordLeastTwoDigits= ChekPasswordLeastTwoDigits(password, isValidPassword);
  15.             ChekPasswordValid(isValidChekPasswordLength, isValidChekPasswordConsistLettersDigits, isValidChekPasswordLeastTwoDigits);
  16.         }
  17.  
  18.         static bool ChekPasswordLength(string password, bool isValidPassword)
  19.         {
  20.             if (password.Length < 6 || password.Length > 10)
  21.             {
  22.                 Console.WriteLine("Password must be between 6 and 10 characters");
  23.                 isValidPassword = false;
  24.             }
  25.             return isValidPassword;
  26.         }
  27.  
  28.         static bool ChekPasswordConsistLettersDigits(string password, bool isValidPassword)
  29.         {
  30.             double passwordLettersDigits = 0;
  31.             for (int i = 0; i < password.Length; i++)
  32.             {
  33.                 char symbol = password[i];
  34.  
  35.                 if ((password[i] >= 48 && password[i] <= 57) ||
  36.                     (password[i] >= 65 && password[i] <= 90) ||
  37.                     (password[i] >= 97 && password[i] <= 122))
  38.                 {
  39.                     passwordLettersDigits++;
  40.                 }
  41.             }
  42.             if (passwordLettersDigits != password.Length)
  43.             {
  44.                 Console.WriteLine("Password must consist only of letters and digits");
  45.                 isValidPassword = false;
  46.             }
  47.             return isValidPassword;
  48.         }
  49.  
  50.         static bool ChekPasswordLeastTwoDigits(string password, bool isValidPassword)
  51.         {
  52.             double passwordLeastTwoDigits = 0;
  53.  
  54.             for (int i = 0; i < password.Length; i++)
  55.             {
  56.                 char symbol = password[i];
  57.                 if (password[i] >= 48 && password[i] <= 57)
  58.                 {
  59.                     passwordLeastTwoDigits++;
  60.                 }
  61.             }
  62.             if (passwordLeastTwoDigits < 2)
  63.             {
  64.                 Console.WriteLine("Password must have at least 2 digits");
  65.                 isValidPassword = false;
  66.             }
  67.             return isValidPassword;
  68.         }
  69.  
  70.         static void ChekPasswordValid(bool isValidChekPasswordLength,
  71.             bool isValidChekPasswordConsistLettersDigits,
  72.             bool ChekPasswordLeastTwoDigits)
  73.         {
  74.             if (isValidChekPasswordLength==true &&
  75.             isValidChekPasswordConsistLettersDigits==true &&
  76.             ChekPasswordLeastTwoDigits == true)
  77.             {
  78.                 Console.WriteLine("Password is valid");
  79.             }  
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment