将正则表达式验证器组合成自定义验证器 (C#)
我有三个正则表达式验证器,它们针对相同的文本框,但分别给出不同的错误消息。
如何将它们组合在自定义验证器中以返回不同的 ErrorMessage?
public void PasswordValidate(Object source, ServerValidateEventArgs args)
{
Regex PasswordComplexity = new Regex(@"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$");
Regex ConsecutiveCharCheck = new Regex(@"^(?!.*(?:(.)\1{3,})).*$");
Regex PasswordLiteralCheck = new Regex(@"^((?!(p|P)(a|A)(s|S)(s|S)(w|W)(o|O)(r|R)(d|D)).)*$");
I have three regular expression validators which targets the same textbox but gives different error messages respectively.
How do I combine them in a custom validator to return different ErrorMessage?
public void PasswordValidate(Object source, ServerValidateEventArgs args)
{
Regex PasswordComplexity = new Regex(@"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$");
Regex ConsecutiveCharCheck = new Regex(@"^(?!.*(?:(.)\1{3,})).*$");
Regex PasswordLiteralCheck = new Regex(@"^((?!(p|P)(a|A)(s|S)(s|S)(w|W)(o|O)(r|R)(d|D)).)*$");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该将它们保留为三个独立的验证器。但是,如果您想将密码验证与表单验证的其余部分分开,那么您可以查看
ValidationGroup
属性用于对其输出进行分组。编辑:根据下面的评论,我认为首选解决方案是将
Display
属性设置为 Dynamic 并保留多个 RegularExpressionValidator。I think you should keep them as the three separate validators. However, if you want to separate password validation from the remainder of the form's validation, then you might look into the
ValidationGroup
property for grouping their output.Edit: Based on comments below, I believe the preferred solution was to set the
Display
property to Dynamic and keep the multiple RegularExpressionValidator's.