Spocoman

04. Password Validator

Jan 26th, 2022 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace PasswordValidator
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string password = Console.ReadLine();
  12.  
  13.             if (CheckResult(password))
  14.             {
  15.                 Console.WriteLine("Password is valid");
  16.             }
  17.             else
  18.             {
  19.                 if (!CheckLenght(password))
  20.                 {
  21.                     Console.WriteLine("Password must be between 6 and 10 characters");
  22.                 }
  23.                 if (!CheckSymbol(password))
  24.                 {
  25.                     Console.WriteLine("Password must consist only of letters and digits");
  26.                 }
  27.                 if (!CheckDigits(password))
  28.                 {
  29.                     Console.WriteLine("Password must have at least 2 digits");
  30.                 }
  31.             }
  32.         }
  33.  
  34.         static bool CheckResult(string pass)
  35.         {
  36.             return CheckLenght(pass) && CheckSymbol(pass) && CheckDigits(pass);
  37.         }
  38.  
  39.         static bool CheckLenght(string pass)
  40.         {
  41.             return pass.Length >= 6 && pass.Length <= 10;
  42.         }
  43.  
  44.         static bool CheckSymbol(string pass)
  45.         {
  46.             return new Regex("[a-zA-Z0-9 -]+").Replace(pass, "") == "";
  47.         }
  48.  
  49.         static bool CheckDigits(string pass)
  50.         {
  51.             return pass.Count(x => x >= '0' && x <= '9') >= 2;
  52.         }
  53.     }
  54. }
Add Comment
Please, Sign In to add comment