用于检查密码有效性的 C# 程序


创建密码时,你可能已经看到用户验证要求,比如密码应该是强壮的,并且包含 -

  • 最小 8 个字符,最大 14 个字符
  • 一个是小写字母
  • 不含空格
  • 一个是大写字母
  • 一个特殊字符

我们逐一了解如何检查条件 -

最小 8 个字符,最大 14 个字符

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

至少一个是小写字母

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

Learn C# in-depth with real-world projects through our C# certification course. Enroll and become a certified expert to boost your career.

不含空格

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

一个是大写字母

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

检查一个特殊字符

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

更新于: 19-6-2020

820 浏览

开启您的 事业

通过完成课程获取认证

立即开始
Advertisement