Skip to content

Instantly share code, notes, and snippets.

@johnifegwu
Created April 15, 2020 20:27
Show Gist options
  • Save johnifegwu/d80221e9f3793c6a6fe875845d13c7f1 to your computer and use it in GitHub Desktop.
Save johnifegwu/d80221e9f3793c6a6fe875845d13c7f1 to your computer and use it in GitHub Desktop.
Password Validator (C# Implementation)
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
//Author: John Ifegwu
namespace MICKLE.Security
{
public class PasswordValidator
{
static void Main(string[] args)
{
Console.WriteLine("Enter password: \n Minimum length is 5 \n Maximum length is 10 \n Must contain at least one special character and one number.");
string pw = "";
pw = Console.ReadLine();
if (ValidatePassword(pw,5,10))
{
Console.WriteLine("Congratulations, your password is valid.");
}
else
{
Console.WriteLine("Your password is not valid.");
}
}
static bool ValidatePassword(string pw, int minL, int maxL)
{
bool IsFullLength = false;
bool HasSpcChar = false;
bool HasNum = false;
bool NoWhiteSp = false;
//Check length
if (pw.Length >= minL && pw.Length <= maxL)
{
IsFullLength = true;
}
//Check number
for (int x = 0; x <10; x++)
{
if (pw.Contains(Convert.ToString(x)))
{
HasNum = true;
break;
}
}
//Check special characters
char[] spChars = {'!','@','#','$','%','^','&','*','~','<','>','?','+','×','÷','=','/','_','€','£','¥','₩','\\','|'};
foreach (char x in spChars)
{
if (pw.Contains(x))
{
HasSpcChar = true;
break;
}
}
//Check white spaces
if (! pw.Contains(" "))
{
NoWhiteSp = true;
}
return (HasSpcChar && HasNum && IsFullLength && NoWhiteSp);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment