Skip to content

Instantly share code, notes, and snippets.

@Retterath
Created October 21, 2018 18:13
Show Gist options
  • Save Retterath/f111ab7426055d28761f73df4cd3d808 to your computer and use it in GitHub Desktop.
Save Retterath/f111ab7426055d28761f73df4cd3d808 to your computer and use it in GitHub Desktop.
Password validator (experiment)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Password_Validator
{
class Program
{
public static void Main()
{
string password = Console.ReadLine();
bool isBetween6and10 = CheckLengthOfPassword(password);
if (isBetween6and10 == false)
{
Console.WriteLine("Password must be between 6 and 10 characters");
}
bool containsOnlyDigitsAndLetters = ContainsOnlyDigitsAndLetters(password);
if (containsOnlyDigitsAndLetters == false)
{
Console.WriteLine("Password must consist only of letters and digits");
}
bool containsMinimum2Digits = CheckMinDigit(password);
if (containsMinimum2Digits == false)
{
Console.WriteLine("Password must have at least 2 digits");
}
if (isBetween6and10 && containsOnlyDigitsAndLetters && containsMinimum2Digits)
{
Console.WriteLine("Password is valid");
}
}
private static bool CheckMinDigit(string password)
{
int count = 0;
for (int i = 0; i < password.Length; i++)
{
char symbol = password[i];
if (char.IsDigit(symbol))
{
count++;
}
}
return count >= 2 ? true : false;
}
private static bool ContainsOnlyDigitsAndLetters(string password)
{
for (int i = 0; i < password.Length; i++)
{
char symbol = password[i];
if (!char.IsDigit(symbol) || !char.IsLetter(symbol))
{
return false;
}
}
return true;
}
private static bool CheckLengthOfPassword(string password)
{
return password.Length >= 6 && password.Length <= 10 ? true : false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment