简体   繁体   English

如何在ASP.NET Web窗体正则表达式验证器中使用多个正则表达式

[英]How to use multiple Regular expressions in ASP.NET Web Forms Regular Expression validator

我需要针对多个正则表达式来验证某些控件的输入,如何使用一个正则表达式验证器控件来做到这一点。

There's no way. 不可能。 You need a regular expression validator for each regex you want to test. 您需要为要测试的每个正则表达式使用一个正则表达式验证器。

Try to build a regular expression using the OR operator. 尝试使用OR运算符构建正则表达式。 Check this links for more information about it: 检查此链接以获取有关此信息的更多信息:

http://forums.asp.net/t/1213089.aspx?Multiple+Format+in+Regular+Expression+Validator https://msdn.microsoft.com/en-us/library/aa976858(v=vs.71).aspx http://forums.asp.net/t/1213089.aspx?Multiple+Format+in+Regular+Expression+Validator https://msdn.microsoft.com/zh-CN/library/aa976858(v=vs.71 ).aspx

If it's not possible to adapt and build a regular expression acording to your needs, you can use a custom validator and make all the validatations for each regular expression that you could need. 如果无法根据您的需要来适应和构建正则表达式,则可以使用自定义验证器,并对您可能需要的每个正则表达式进行所有验证。 Check this link for more information about Custom Validator. 检查此链接以获取有关“定制验证器”的更多信息。

I think the only solution to your problem is to use a custom validator.check the below code.this is just to give you an idea about how to use custom validator. 我认为解决问题的唯一方法是使用自定义验证器,请检查以下代码,这只是给您一个有关如何使用自定义验证器的想法。

<asp:TextBox runat="server" ID="UserName" />                            
<asp:CustomValidator ID="CustomValidator1" runat="server" 
ControlToValidate="UserName" ></asp:CustomValidator>

protected void ValidateUser(object source, ServerValidateEventArgs args)
        {
            Regex regx = new Regex("^[a-zA-Z0-9]{6,}$");
            Regex regx2 = new Regex("^[a-zA-Z0-9]{6,}$");
            if (regx.IsMatch(UserName.Text) == false)
            {
                CustomValidator1.ErrorMessage = "error message";
                args.IsValid = false;
            }
            else if(regx2.IsMatch(UserName.Text) == false) 
            {
                CustomValidator1.ErrorMessage = "second error message";
                args.IsValid = false;
            }
          else
           {args.IsValid = true;}
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM