Advertisement
Qrist

Password Validator

Apr 14th, 2020
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System;
  2. class Program
  3. {
  4.     static void Main(string[] args)
  5.     {
  6.         string pass = Console.ReadLine();
  7.         Check(pass);
  8.     }
  9.     public static bool Characters(string password)        
  10.     {
  11.         return password.Length >= 6 && password.Length <= 10;
  12.     }
  13.     public static bool Consists(string password)
  14.     {
  15.         for (int i = 0; i < password.Length; i++)
  16.         {
  17.             if(!char.IsLetterOrDigit(password[i]))
  18.             {
  19.                 return false;
  20.             }
  21.         }
  22.         return true;
  23.     }
  24.     public static int Digits(string password)
  25.     {
  26.         int count = 0;
  27.         for (int i = 0; i < password.Length; i++)
  28.         {
  29.             if(char.IsDigit(password[i]))
  30.             {
  31.                 count++;
  32.             }
  33.         }
  34.         return count;
  35.     }
  36.     public static void Check(string password)
  37.     {
  38.         bool valid = true;
  39.         for (int i = 0; i < password.Length; i++)
  40.         {
  41.             if (!Characters(password))
  42.             {
  43.                 valid = false;
  44.                 Console.WriteLine("Password must be between 6 and 10 characters");
  45.             }
  46.             if (!Consists(password))
  47.             {
  48.                 valid = false;
  49.                 Console.WriteLine("Password must consist only of letters and digits");
  50.             }
  51.             if (Digits(password) < 2)
  52.             {
  53.                 valid = false;
  54.                 Console.WriteLine("Password must have at least 2 digits");
  55.             }
  56.             if (!valid)
  57.             {
  58.                 return;
  59.             }
  60.         }if(valid)
  61.         {
  62.             Console.WriteLine("Password is valid");
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement