.Net Core=我想为UserValidator添加个性规则

发布于 2025-01-10 12:01:00 字数 410 浏览 0 评论 0原文

例如; 我在表中有密码列。任何人进入系统时都必须写入密码。我想要包含至少一个字符的密码,例如(/,-,?,+,!)。我做了这样一段代码;

public UserValidator()
{
    RuleFor(p => p.Password).MinimumLength(10);
    RuleFor(p => p.Password).Must(MustBeCharacter);
}
private bool MustBeCharacter(string arg)
{
    return arg.Contains("."+","+"?"+"!"+"*"+"-"+"+");
}

我有一个问题。系统需要所有这些(“.”+“,”+“?”+“!”+“*”+“-”+“+”)。但我至少还想要一份。我该怎么做这个规则。 太感谢了

For Example;
I have password column in the table. AnyBody while enter system must write password. I want contain password minumum one character such as(/,-,?,+,!). I did such one code;

public UserValidator()
{
    RuleFor(p => p.Password).MinimumLength(10);
    RuleFor(p => p.Password).Must(MustBeCharacter);
}
private bool MustBeCharacter(string arg)
{
    return arg.Contains("."+","+"?"+"!"+"*"+"-"+"+");
}

I take a problem. system want all of them ("."+","+"?"+"!"+"*"+"-"+"+"). but I want minimum one more. How can I do this rule.
Thank you so much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与昨日 2025-01-17 12:01:00
  • 在当前的方法中,您将所有特殊字符连接成一个字符串并应用 Contains() 就可以了。这就是原因,系统期望所有特殊字符都应该是您的 arg 字符串的一部分。

  • 您应该在数组或列表中定义所有特殊字符,然后使用 .Any() 检查字符串 arg 中至少应出现一辆特殊汽车,

    private bool MustBeCharacter(字符串参数)
    { 
        varspecialChars = ".,?!*-+".ToCharArray();
        返回specialChars.Any(x => arg.Contains(x));
    }
    

在线尝试:.Net Fiddle

  • In your current approach, you are concatenating all special characters into a single string and applying Contains() on it. That is the reason, the system expects all special chars should be part of your arg string.

  • You should define all special characters inside an array or a list, then use .Any() to check at least one special car should present in the string arg,

    private bool MustBeCharacter(string arg)
    { 
        var specialChars = ".,?!*-+".ToCharArray();
        return specialChars.Any(x => arg.Contains(x));
    }
    

Try online: .Net Fiddle

看透却不说透 2025-01-17 12:01:00

系统需要所有这些字符,因为您正在查找整个字符串而不是一个字符。

你首先需要数组

private string[] arr = [".",",","?","!","*","-","+"];
private bool MustBeCharacter(string arg)
{  
        return arr.Any(s => args.Contains(s));
    
}

System want all of them because you are looking for entire string and not one character.

you need array first

private string[] arr = [".",",","?","!","*","-","+"];
private bool MustBeCharacter(string arg)
{  
        return arr.Any(s => args.Contains(s));
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文