如何将多个正则表达式与不同的验证消息一起使用

编程入门 行业动态 更新时间:2024-10-26 17:35:46
本文介绍了如何将多个正则表达式与不同的验证消息一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

要求

我想通过使用多个正则表达式来检查密码策略.对于每个违反政策的行为,我想显示一条特定的验证消息.

I want to check password policies by using multiple regex expressions. For each policy violation I want to display a specific validation message.

示例:

  • 您需要至少使用2个数字
  • 您需要至少使用一个大写字母和一个小写字母
  • 您至少需要使用8个字母
  • ...

尝试

我尝试使用多个正则表达式(Fluent Validation 匹配(字符串表达式)),但是ASP.NET MVC不允许具有多个正则表达式.

I tried to use multiple regex expressions (Fluent Validation Match(string expression)), but ASP.NET MVC does not allow to have multiple regex expressions.

多次看到以下验证类型:regex

The following validation type was seen more than once: regex

问题

如何在Fluent验证中使用多个正则表达式验证器?

How can I use multiple regex validators in Fluent Validation?

推荐答案

您可以使用在抽象验证器中定义的自定义方法:

You can use custom method defined in Abstract validator:

public class UserValidator : AbstractValidator<User> { public UserValidator () { Custom(user => { Regex r1 = define regex that validates that there are at least 2 numbers Regex r2 = define regex for upper and lower case letters string message = string.Empty; if(!r1.IsMatch(user.password)) { message += "You need to use at least 2 numbers."; } if(!r2.IsMatch(user.password)) { message += "You need to use at least one upper and one lower case letter."; } return message != string.Empty; ? new ValidationFailure("Password", message ) : null; }); } }

更多推荐

如何将多个正则表达式与不同的验证消息一起使用

本文发布于:2023-10-17 13:20:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1501043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   如何将   消息   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!