.Net Core=我想为UserValidator添加个性规则
例如; 我在表中有密码列。任何人进入系统时都必须写入密码。我想要包含至少一个字符的密码,例如(/,-,?,+,!)。我做了这样一段代码;
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在当前的方法中,您将所有特殊字符连接成一个字符串并应用
Contains()
就可以了。这就是原因,系统期望所有特殊字符都应该是您的arg
字符串的一部分。您应该在数组或列表中定义所有特殊字符,然后使用
.Any()
检查字符串arg
中至少应出现一辆特殊汽车,在线尝试:.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 yourarg
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 stringarg
,Try online: .Net Fiddle
系统需要所有这些字符,因为您正在查找整个字符串而不是一个字符。
你首先需要数组
System want all of them because you are looking for entire string and not one character.
you need array first