Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Password_Validator
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             ValidatePassword(Console.ReadLine());
  10.         }
  11.  
  12.         static void ValidatePassword(string password)
  13.         {
  14.             string validation = ValidatePassBetweenSixAndTenCharacters(password);
  15.             if (validation != null)
  16.             {
  17.                 Console.WriteLine(validation);
  18.             }
  19.  
  20.             string validationTwo = ValidateContainsOnlyDigitsAndLetters(password);
  21.             if (validationTwo != null)
  22.             {
  23.                 Console.WriteLine(validationTwo);
  24.             }
  25.            
  26.             string validationThree = ValidateHasLeastTwoDigits(password);
  27.             if (validationThree != null)
  28.             {
  29.                 Console.WriteLine(validationThree);
  30.             }
  31.  
  32.             if (validation == null && validationTwo == null && validationThree == null)
  33.             {
  34.                 Console.WriteLine("Password is valid");
  35.             }
  36.         }
  37.  
  38.         static string ValidatePassBetweenSixAndTenCharacters(string password)
  39.         {
  40.             int length = password.Length;
  41.             string res = null;
  42.             if(length < 6 || length > 10)
  43.             {
  44.                 res = "Password must be between 6 and 10 characters";
  45.             }
  46.             return res;
  47.         }
  48.  
  49.         static string ValidateContainsOnlyDigitsAndLetters(string password)
  50.         {
  51.             string res = null;
  52.  
  53.             for (int i = 0; i < password.Length; i++)
  54.             {
  55.                 if(!char.IsLetterOrDigit(password[i]))
  56.                 {
  57.                     res = "Password must consist only of letters and digits";
  58.                     break;
  59.                 }
  60.             }
  61.  
  62.             return res;
  63.         }
  64.  
  65.         static string ValidateHasLeastTwoDigits(string password)
  66.         {
  67.             int counter = 0;
  68.             string res = null;
  69.             foreach (char c in password)
  70.             {
  71.                 if (char.IsDigit(c))
  72.                 {
  73.                     counter++;
  74.                 }
  75.  
  76.                 if (counter == 2)
  77.                 {
  78.                     break;
  79.                 }
  80.             }
  81.  
  82.             if (counter < 2)
  83.             {
  84.                 res = "Password must have at least 2 digits";
  85.             }
  86.  
  87.             return res;
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement