ASP.net MVC 正则表达式验证不起作用

发布于 2024-11-07 08:53:52 字数 696 浏览 1 评论 0原文

我有一个包含许多验证规则的模型,除了用于验证邮政编码的规则外,所有这些规则都运行良好。规则/属性定义如下:

        [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书间行客 2024-11-14 08:53:52

一些注意事项:

  • 您没有为 [0-9R] 组中的 R 或空格之前的最后一个 [0-9A-Z] 字符提供小写选项。除非您使用“忽略大小写”选项,否则这些选项将仅匹配大写项目。如果您使用“忽略大小写”,则所有其他小写范围都是不必要的。
  • 空格周围的括号是可选的。这 ?运算符匹配前一个字符,因此仅当集合中可能有多个字符时才需要括号。
  • 我上面质疑的语法不标准。我使用正则表达式已经有 10 年了,但我从未见过这样的事情。它“有效”,具体取决于您的解析器,但甚至没有记录在它工作的 .NET 解析器中。请注意,它完全失败了 - 它甚至无法正确解析正则表达式,更不用说在 Javascript 中找到任何匹配项了。最好的方法是使用 [ABD-HJLNP-UW-Zabd-hjlnp-uw-z] 来代替,这种方法应该得到全面支持。 RegexPal 上有一个 Javascript 正则表达式测试器,我验证了您的表达式在那里失败,但通过这个简单的更改它就成功了。
  • 与上一个相关,您没有指出验证实际发生的位置。如果您使用客户端验证器,它实际上是在 Javascript 中运行,而不是在 .NET 中运行。这绝对可以解释为什么它在这个表达式上失败。

TL;DR:尝试替换最终的字符模式,看看它是否开始工作。

Some notes:

  • You have not provided lower-case options for the R in the [0-9R] group or for the final [0-9A-Z] character before the space. Unless you're using an Ignore Case option, those will only match upper case items. And if you're using Ignore Case, then all of the other lower-case ranges are unnecessary.
  • The brackets around the space are optional. The ? operator matches the previous character, so the brackets would only be necessary if you had more than one character that might be in the set.
  • The syntax that I questioned above is not standard. I've been using regular expressions for 10 years, and I've never seen anything like this. It "works", depending on your parser, but isn't even documented in the .NET parser where it works. Note that it fails completely - it doesn't even parse the regular expression correctly, much less find any matches - in Javascript. The best way to do this, which should be supported across the board, is to use [ABD-HJLNP-UW-Zabd-hjlnp-uw-z] instead. There's a Javascript regex tester at RegexPal, and I verified that your expression fails there but with this simple change it succeeds.
  • Related to the previous, you haven't indicated where the validation actually occurs. If you're using a client-side validator, it's actually running it in Javascript, not .NET. That could definitely explain why it's failing on this expression.

TL;DR: Try replacing the final character pattern and see if it starts working.

凉薄对峙 2024-11-14 08:53:52

我已经运行了这个测试

 var attr = new RegularExpressionAttribute(@"[A-Za-z]{1,2}[0-9R][0-9A-Z]?[ ]?[0-9][A-Za-z-[CIKMOVcikmov]]{2}");
 Assert.IsTrue(attr.IsValid("IP4 4DL"));

,看起来效果很好。

我唯一的建议是检查传入的邮政编码两侧是否没有空格。

I've run this test

 var attr = new RegularExpressionAttribute(@"[A-Za-z]{1,2}[0-9R][0-9A-Z]?[ ]?[0-9][A-Za-z-[CIKMOVcikmov]]{2}");
 Assert.IsTrue(attr.IsValid("IP4 4DL"));

and it seems to work fine.

My only suggestion is to check that there's no whitespace either side of the incoming postcode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文