Home  >  Article  >  Backend Development  >  C# program to check password validity

C# program to check password validity

王林
王林forward
2023-08-21 21:17:171378browse

When creating a password, you may have seen verification requirements on the website, such as the password should be strong and contain the following requirements:

  • minimum of 8 characters, Up to 14 characters
  • At least one lowercase letter
  • No spaces
  • At least one uppercase letter
  • At least one special character

Let’s check these conditions one by one −

Minimum 8 characters, maximum 14 characters

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

At least one lowercase letter

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

No whitespace

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

A capital letter

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

Check if there is a special character

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

The above is the detailed content of C# program to check password validity. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete