Advertisement
ElviraPetkova

Password validator

Feb 14th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _4.Password_Validator
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string password = Console.ReadLine();
  14.             PrintPasswordValidator(password);
  15.         }
  16.  
  17.         static void PrintPasswordValidator(string password)
  18.         {
  19.             if (CorrectLenght(password) && CorrectContent(password) && CorrectDigits(password))
  20.             {
  21.                 Console.WriteLine("Password is valid");
  22.             }
  23.             else
  24.             {
  25.                 if (!CorrectLenght(password))
  26.                 {
  27.                     Console.WriteLine("Password must be between 6 and 10 characters");
  28.                 }
  29.                 if (!CorrectContent(password))
  30.                 {
  31.                     Console.WriteLine("Password must consist only of letters and digits");
  32.                 }
  33.                 if (!CorrectDigits(password))
  34.                 {
  35.                     Console.WriteLine("Password must have at least 2 digits");
  36.                 }
  37.             }
  38.         }
  39.  
  40.         static bool CorrectLenght(string password)
  41.         {
  42.             int lenght = password.Length;
  43.             bool isCorrectLenght = true;
  44.             if (lenght < 6 || lenght > 10)
  45.             {
  46.                 isCorrectLenght = false;
  47.             }
  48.  
  49.             return isCorrectLenght;
  50.         }
  51.  
  52.         static bool CorrectContent(string password)
  53.         {
  54.             int otherSymbols = 0;
  55.             bool onlyLettersAndDigits = true;
  56.  
  57.             for (int countOfWord = 0; countOfWord < password.Length; countOfWord++)
  58.             {
  59.                 //тук за по-кратко може да се използва char.IsLetterOrDigit
  60.                 if (!(password[countOfWord] >= 'A' && password[countOfWord] <= 'Z')
  61.                      && !(password[countOfWord] >= 'a' && password[countOfWord] <= 'z')
  62.                      && !(password[countOfWord] >= '0' && password[countOfWord] <= '9'))
  63.                 {
  64.                     otherSymbols++;
  65.                 }
  66.             }
  67.  
  68.             if (otherSymbols > 0)
  69.             {
  70.                 onlyLettersAndDigits = false;
  71.             }
  72.  
  73.             return onlyLettersAndDigits;
  74.         }
  75.  
  76.         static bool CorrectDigits(string password)
  77.         {
  78.             int countOfDigits = 0;
  79.             bool twoDigits = true;
  80.  
  81.             for (int i = 0; i < password.Length; i++)
  82.             {
  83.                 if (password[i] >= '0' && password[i] <= '9')
  84.                 {
  85.                     countOfDigits++;
  86.                 }
  87.             }
  88.  
  89.             if (countOfDigits < 2)
  90.             {
  91.                 twoDigits = false;
  92.             }
  93.  
  94.             return twoDigits;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement