简体   繁体   English

asp.net Regularexpressionvalidator无法按预期工作

[英]asp.net regularexpressionvalidator not working as expected

I've the following textbox control and regular expression validator: 我有以下文本框控件和正则表达式验证器:

<asp:TextBox runat="server" ID="ATextBox" style="width:400px;"/>
                <asp:RegularExpressionValidator runat="server" ID="ATextBoxRegularExpressionValidator" Display="Dynamic"
                    ControlToValidate="ATextBox" ValidationExpression="^([a-zA-Z0-9]+)|((https?://)?([\w-]+\.)+[\w-]+(/[-\w ;,./?%&=]*)?)$" 
                    ErrorMessage="<%$ Resources:Antivirus, AVURL_InvalidLink %>">
                                       </asp:RegularExpressionValidator>

The regular expression is: 正则表达式为:

^([a-zA-Z0-9]+)|((https?://)?([\w-]+\.)+[\w-]+(/[-\w ;,./?%&=]*)?)$

which works when I test it with any validator expression testers such as: 当我使用任何验证程序表达式测试器(例如:

http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

Basically the regular expression should accept all URL's and hostnames. 基本上,正则表达式应接受所有URL和主机名。

So good: google google.com http://google.com https://google.com 太好了:google google.com http://google.com https://google.com

Bad: 坏:

<asp>http://google.com</asp>

Anyone any idea why my regular expression when running in the app is failing the following: google.com http://google.com https://google.com 任何人都知道为什么我的正则表达式在应用程序中运行时为什么无法通过以下操作:google.com http://google.com https://google.com

UPDATE UPDATE

I've managed to solve my problem.... the regular expression that is doing what I want to do is: 我已经设法解决了我的问题...。正在做我想做的正则表达式是:

^((https?://)?([\\w-]+.) [\\w-]+(/[-\\w ;,./?%&=] )?)$ ^((https?://)?([\\ w-] +。) [\\ w-] +(/ [-\\ w;,。/?%&=] )?)$

Thanks for your help 谢谢你的帮助

I would say the first problem is that the / need escaped: 我要说的第一个问题是/需要逃脱了:

^([a-zA-Z0-9]+)|((https?:\/\/)?([\w-]+\.)+[\w-]+(\/[-\w ;,.\/?%&=]*)?)$

The next problem of interest is that the Regex you currently have doesn't really ever match the entire URL, it matches and groups portions. 下一个有趣的问题是您当前使用的正则表达式实际上并不会匹配整个URL,它会匹配并分组部分。 But maybe that's what you want. 但这也许就是您想要的。

Here is a Rubular with the modified Regex and values you noted. 这是带有您修改过的Regex和值的Rubular

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

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