C# program to check the validity of a Password


For validity of a password, you need to recall the concept when your create a password to signup to a website.

While creating a password, you may have seen the validation requirements on a website like a password should be strong and have −

  • Min 8 char and max 14 char

  • One upper case

  • One special char

  • One lower case

  • No white space

Let us see how to check the conditions one by one.

Min 8 char and max 14 char

if (passwd.Length < 8 || passwd.Length > 14)
return false;

One upper case

if (!passwd.Any(char.IsUpper))
return false;

Atleast one lower case

if (!passwd.Any(char.IsLower))
return false;

No white space

if (passwd.Contains(" "))
return false;

Check for one special character

string specialCh = @"%!@#$%^&*()?/>.<,:;'\|}]{[_~`+=-" + "\"";
char[] specialCh = specialCh.ToCharArray();
foreach (char ch in specialChArray) {
   if (passwd.Contains(ch))
   return true;
}

Updated on: 22-Jun-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements