TheBulgarianWolf

Password Validator

Oct 31st, 2020
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleApp1
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Console.WriteLine("-------------WELCOME TO PASSWORD VALIDATOR---------------");
  11.             Console.Write("Enter your password here: ");
  12.             string password = Console.ReadLine();
  13.             bool digitsValidation = DigitValidator(password);
  14.             bool lengthValidation = LengthValidator(password);
  15.             bool charsValidation = CharsValidator(password);
  16.             if(digitsValidation == false)
  17.             {
  18.                 Console.WriteLine("Password must have at least 2 digits.");
  19.             }
  20.  
  21.             if(lengthValidation == false)
  22.             {
  23.                 Console.WriteLine("Password must be between 6 and 10 characters.");
  24.             }
  25.  
  26.             if(charsValidation == false)
  27.             {
  28.                 Console.WriteLine("Password must consist only of letters and digits.");
  29.             }
  30.  
  31.             if(charsValidation == true && lengthValidation == true && digitsValidation == true)
  32.             {
  33.                 Console.WriteLine("The password is valid.");
  34.             }
  35.         }
  36.  
  37.         static bool DigitValidator(string password)
  38.         {
  39.             int digitCounter = 0;
  40.             foreach(char i in password)
  41.             {
  42.                 if (i >= '0' && i <= '9')
  43.                 {
  44.                     digitCounter++;
  45.                 }
  46.                    
  47.  
  48.             }
  49.             if (digitCounter < 2)
  50.             {
  51.                 return false;
  52.             }
  53.             else
  54.             {
  55.                 return true;
  56.             }
  57.         }
  58.  
  59.         static bool LengthValidator(string password)
  60.         {
  61.             if (password.Length < 6 && password.Length > 10)
  62.             {
  63.                 return false;
  64.             }
  65.             else
  66.             {
  67.                 return true;
  68.             }
  69.         }
  70.  
  71.         static bool CharsValidator(string password)
  72.         {
  73.             bool validation = true;
  74.             foreach(char i in password)
  75.             {
  76.                 if((i>=48 && i<=57) || (i>=65 && i<=90) || (i>=97 && i <= 122))
  77.                 {
  78.                     continue;
  79.                 }
  80.                 else
  81.                 {
  82.                     validation = false;
  83.                     return validation;
  84.                 }
  85.             }
  86.  
  87.             return validation;
  88.         }
  89.     }
  90. }
  91.  
Add Comment
Please, Sign In to add comment