c#正则表达式模型验证长度

本文关键字:验证 模型 正则表达式 | 更新日期: 2023-09-27 18:15:48

问题:我怎么写这个不放入所有可能的情况?如果有人有一天得到4个密码要求,它将很容易投入,但很难调试,因为将有4个!不同的工作方式。

如何在代码中使用:

[RegularExpression("^[A-Za-z0-9!@#$]*([A-Za-z][0-9][!@#$]|[0-9][!@#$][A-Za-z]|[!@#$][A-Za-z][0-9]|[!@#$][0-9][A-Za-z]|[0-9][A-Za-z][!@#$]|[A-Za-z][!@#$][0-9])[A-Za-z0-9!@#$]*$", ErrorMessage = "The Password value must contain at least one letter, one digit, and one special character.")]

为了方便观看,我打破了它,所以不需要滚动:

[RegularExpression("^[A-Za-z0-9!@#$]*
([A-Za-z][0-9][!@#$]|[0-9][!@#$][A-Za-z]|
[!@#$][A-Za-z][0-9]|[!@#$][0-9][A-Za-z]|
[0-9][A-Za-z][!@#$]|[A-Za-z][!@#$][0-9])
[A-Za-z0-9!@#$]*$",
ErrorMessage = "The Password value must contain at least one letter,
one digit, and one special character.")]

你可以看到这是3!中间()的可能性用| (OR)隔开,所以从这里你可以看到4!可能性将很难维持。

问题要求:我希望c#中的正则表达式做的基本事情是要求(没有特定的顺序)至少一个字母,至少一个数字,至少一个特殊字符!,@,#或$

c#正则表达式模型验证长度

One Regex

您可以使用零宽度正向预看断言(参见MSDN的"正则表达式语言-快速参考"-在分组构造部分)来检查您的and密码要求,而不必显式地覆盖所需密码组件可能出现的所有顺序:

^(?=.*?[a-zA-Z])(?=.*?[0-9])(?=.*?[!@#$]).{3,}$

操作如下:

  • (?=.*?[a-zA-Z]) -前面发现一个alpha字符
  • (?=.*?[0-9]) - 前面发现的数字
  • (?=.*?[!@#$]) - 前面允许的特殊字符
  • .{3,} -总共三个或三个以上字符

我在此基础上分享了一个正则表达式,你可能会觉得有用。

属性-标准密码检查器

根据@LeffeBrune的评论,您应该考虑使用专用的密码检查器类进行检查,该类会根据每个条件暴露一个属性。

例如,这里有一个快速的&

void Main()
{
    var passwords = new string[] {"password", "passw0rd", "passw0rd!", "123456", "!#@$"};
    foreach (var pw in passwords)
    {
        var checker = new PasswordChecker(pw);
        var isValid = checker.IsValid;
        Console.WriteLine("Password {0} is {1}valid.", pw, isValid ? "" : "in");
        if (!isValid)
        {
            Console.WriteLine("    Has alpha?  {0}", checker.HasAlpha ? "Yes." : "NO!");
            Console.WriteLine("    Has number?  {0}", checker.HasNumber ? "Yes." : "NO!");
            Console.WriteLine("    Has special?  {0}", checker.HasSpecial ? "Yes." : "NO!");
            Console.WriteLine("    Has length?  {0}", checker.HasLength ? "Yes." : "NO!");
        }
    }
}
public class PasswordChecker
{
    public const int MINIMUM_LENGTH = 3;
    private string _password;
    public PasswordChecker(string password)
    {
        _password = password;
    }
    public bool HasAlpha
    {
        get
        {
            return Regex.Match(_password, "(?=.*?[a-zA-Z])").Success;
        }
    }
    public bool HasNumber
    {
        get
        {
            return Regex.Match(_password, "(?=.*?[0-9])").Success;
        }
    }
    public bool HasSpecial
    {
        get
        {
            return Regex.Match(_password, "(?=.*?[!@#$])").Success;
        }
    }
    public bool HasLength
    {
        get
        {
            return _password.Length >= MINIMUM_LENGTH;
        }
    }
    public bool IsValid
    {
        get
        {
            return HasLength && HasAlpha && HasNumber && HasSpecial;
        }
    }
}

…生成以下输出:

Password password is invalid.
    Has alpha?  Yes.
    Has number?  NO!
    Has special?  NO!
    Has length?  Yes.
Password passw0rd is invalid.
    Has alpha?  Yes.
    Has number?  Yes.
    Has special?  NO!
    Has length?  Yes.
Password passw0rd! is valid.
Password 123456 is invalid.
    Has alpha?  NO!
    Has number?  Yes.
    Has special?  NO!
    Has length?  Yes.
Password !#@$ is invalid.
    Has alpha?  NO!
    Has number?  NO!
    Has special?  Yes.
    Has length?  Yes.

你可以用这个快速的&当然,更深入地使用dirty PoC,但是了解哪些标准或标准失败的好处是希望清楚的。