ASP.net MVC 正则表达式验证不起作用
我有一个包含许多验证规则的模型,除了用于验证邮政编码的规则外,所有这些规则都运行良好。规则/属性定义如下:
[Required(ErrorMessage="Postcode is required")]
[StringLength(20, ErrorMessage="Postcode must be under 20 characters")]
[RegularExpression(@"[A-Za-z]{1,2}[0-9R][0-9A-Z]?[ ]?[0-9][A-Za-z-[CIKMOVcikmov]]{2}", ErrorMessage="Postcode is not valid")]
public string Postcode { get; set; }
每次邮政编码未通过正则表达式验证时,即使在在线测试器中使用相同的正则表达式(例如 http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx)通过。应该通过的英国邮政编码的几个示例是 IP4 4DL 和 bn35fb
I have a model with a number of validation rules, all of which are working fine with the exception of one designed to validate a postcode. The rule / property is defined as follows:
[Required(ErrorMessage="Postcode is required")]
[StringLength(20, ErrorMessage="Postcode must be under 20 characters")]
[RegularExpression(@"[A-Za-z]{1,2}[0-9R][0-9A-Z]?[ ]?[0-9][A-Za-z-[CIKMOVcikmov]]{2}", ErrorMessage="Postcode is not valid")]
public string Postcode { get; set; }
Every time the postcode fails the RegEx validation, even though using the same RegEx in an online tester (e.g. http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx) passes. a couple of examples of UK postcodes which should pass are IP4 4DL and bn35fb
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些注意事项:
TL;DR:尝试替换最终的字符模式,看看它是否开始工作。
Some notes:
TL;DR: Try replacing the final character pattern and see if it starts working.
我已经运行了这个测试
,看起来效果很好。
我唯一的建议是检查传入的邮政编码两侧是否没有空格。
I've run this test
and it seems to work fine.
My only suggestion is to check that there's no whitespace either side of the incoming postcode.