ASP.NET MVC 2 客户端验证触发不正确
我正在使用 DataAnnotations 在 ASP.NET MVC 2 项目中启用客户端验证。我遇到一个问题,我的 URL 验证正则表达式通过了单元测试,但在实际网站中失败了。
模型
[RegularExpression(UrlValidation.Regex, ErrorMessage = UrlValidation.Message)]
public string Url { get; set; }
Regex = "(([\w]+:)?//)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?"
消息 =“无效 Url”
使用 URL查看
<div class="editor-label">
<%: Html.LabelFor(model => model.Url) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Url) %>
<%: Html.ValidationMessageFor(model => model.Url) %>
</div>
结果 = http://www.chicagoshakes.com/main.taf?p=7,8
通过单元测试
[Test]
public void GetVarUrlPasses()
{
var url = "http://www.chicagoshakes.com/main.taf?p=7,8";
var regex = new Regex(UrlValidation.Regex);
Assert.IsTrue(regex.IsMatch(url));
}
有谁知道为什么这通过了单元测试,但当我在浏览器中测试视图时却失败了?
I am using DataAnnotations to enable client-side validation in an ASP.NET MVC 2 project. I am having an issue where my URL validation regex passes my unit test, but it fails in the actual website.
Model
[RegularExpression(UrlValidation.Regex, ErrorMessage = UrlValidation.Message)]
public string Url { get; set; }
Regex = "(([\w]+:)?//)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?"
Message = "Invalid Url"
View
<div class="editor-label">
<%: Html.LabelFor(model => model.Url) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Url) %>
<%: Html.ValidationMessageFor(model => model.Url) %>
</div>
Result With URL = http://www.chicagoshakes.com/main.taf?p=7,8
Passing Unit Test
[Test]
public void GetVarUrlPasses()
{
var url = "http://www.chicagoshakes.com/main.taf?p=7,8";
var regex = new Regex(UrlValidation.Regex);
Assert.IsTrue(regex.IsMatch(url));
}
Does anyone have any idea why this is passing the unit test, but failing when I test the view in a browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以看到该正则表达式有几个问题,但让您困惑的可能是
p=7,8
中的逗号。您的正则表达式与它不匹配,但IsMatch
不要求它匹配;它非常高兴停在7
处。我猜想客户端验证器隐式锚定了匹配。无论如何,正则表达式应该被锚定;在开头添加^
并在末尾添加$
,您至少应该得到一致的结果。然后您可以更改正则表达式以适应逗号。正则表达式中还有一个拼写错误:
[a-fA-f\d]
中的第二个f
应该是F
。而且,尽管将\d
和\w
放在同一组方括号中并不是错误,但它是多余的;\w
匹配数字和字母,因此您可以删除\d
(并且[\w\d]
可以简化为 <代码>\w)。最后,{2,2}
应该简单地为{2}
。I can see several problems with that regex, but what's tripping you up is probably the comma in
p=7,8
. Your regex doesn't match it, butIsMatch
doesn't require it to; it's perfectly happy stopping at the7
. I would guess that the client-side validator is implicitly anchoring the match. The regex should be anchored anyway; add a^
to the beginning and$
to the end, and you should at least get consistent results. Then you can change the regex to accommodate the comma.There's also a typo in the regex: the second
f
in[a-fA-f\d]
should beF
. And, although it's not an error to have\d
and\w
in the same set of square brackets, it is redundant;\w
matches digits as well as letters, so you can remove the\d
(and[\w\d]
can be reduced to\w
). Finally,{2,2}
should be simply{2}
.因为它正在通过单元测试,所以问题可能出在您的控制器或模型中。
Url
是否曾被直接访问过?Because it's passing the unit test, the problem may be in your controller or model. Is
Url
ever directly accessed?艾伦摩尔基本上是对的,正则表达式是垃圾。我最终使用了这个正则表达式。
另外,这是编写单元测试的正确方法:
Alan Moore was basically right, the regex was junk. I ended up using a this regex.
Also, here's the proper way to write the unit test: