我正在使用 Razor View 引擎开发 MVC 4 ASP.NET 应用程序。我在 View 中有一个名为“Title”的属性,并且已将其元数据设置如下:
[RegularExpression(@"^[\p{L}\p{N}\s\,\،\-\(\)\u200C]*$", ErrorMessage = "Some error message")]
public string Title { get; set; }
在 View 中,只要我开始在相应的文本框中输入任何字符,它就会显示验证消息。这是 Razor View :
@Html.LabelFor(model => model.Title)
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
我错过了什么吗?
最佳答案
客户端验证器使用 javascript,因此正则表达式需要使用特定于 javascript 的语法。
根据 regular-expressions.info ,不支持 \p{...}
结构。
您可以将 \p{L}\p{N}
替换为 \w
,具体取决于您的目标字符集。
关于asp.net - 我的字符串类型域对象的 RegularExpression 属性有奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13698891/