Created
April 15, 2020 20:27
-
-
Save johnifegwu/d80221e9f3793c6a6fe875845d13c7f1 to your computer and use it in GitHub Desktop.
Password Validator (C# Implementation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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