zl程序教程

您现在的位置是:首页 >  Java

当前栏目

struts2 ognl 正则表达式-asp

2023-02-18 16:47:33 时间

  本文介绍了asp.net mvc 4 中的 验证(正则表达式)-剃刀视图的处理方法struts2 ognl 正则表达式,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

  问题描述

  当在正则表达式中使用特殊字符时, 验证器在 asp.net mvc 4 razor 视图中不起作用.

  The not in asp.net mvc 4 razor view, when using the in the .

  型号:

   [StringLength(100)]

    [Display(Description = "First Name")]
    [RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
    public string FirstName { get; set; }

  剃刀视图:

   @Html.TextBoxFor(model => Model.FirstName, new )

    @Html.ValidationMessageFor(model => Model.FirstName)

  不显眼的验证在视图中呈现为:

  The is in view as:

  上述html中的regex模式没有按照Model的中指定的方式呈现,导致即使输入有效数据(Sam's)也会出错.

  The regex in the above html is not as in the Model’s , which in error even when the valid data (Sam's).

  我该如何处理?

  –更新–

  我已经按照@Rick 的建议更新了代码

  I have the code as per @Rick

   [StringLength(100)]

    [Display(Description = "First Name")]
    [RegularExpression("([a-zA-Z0-9 .&'-]+)", ErrorMessage = "Enter only alphabets and numbers of First Name")]
    public string FirstName { get; set; }

  查看源代码显示以下内容:

  View Source shows the :

  我还是有同样的问题.

  推荐答案

  2012 年 7 月 9 日更新 – 看起来这已在 RTM 中修复.

  UPDATE 9 July 2012 – Looks like this is fixed in RTM.

  我们已经暗示了 ^ 和 $ 所以你不需要添加它们.(包含它们似乎没有问题struts2 ognl 正则表达式,但您不需要它们)这似乎是 ASP.NET MVC 4//Beta 中的一个错误.我打开了一个错误

  We imply ^ and $ so you don’t need to add them. (It doesn’t appear to be a to them, but you don’t need them)This to be a bug in ASP.NET MVC 4//Beta. I’ve opened a bug

  查看源码显示如下:

data-val-regex-pattern="([a-zA-Z0-9 .&'-]+)"                  
[1]: https://xuan.ddwoo.top/index.php/archives/540/