asp.net mvc 4 - razor 视图中的 DataAnnotations 验证(正则表达式)
在正则表达式中使用特殊字符时,DataAnnotations 验证器无法在 asp.net mvc 4 razor 视图中工作。
模型:
[StringLength(100)]
[Display(Description = "First Name")]
[RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
public string FirstName { get; set; }
Razor 视图:
@Html.TextBoxFor(model => Model.FirstName, new { })
@Html.ValidationMessageFor(model => Model.FirstName)
不显眼的验证在视图中呈现为:
<input type="text" value="" tabindex="1" style="height:auto;" name="FirstName" maxlength="100" id="FirstName" data-val-regex-pattern="^([a-zA-Z0-9 .&amp;&#39;-]+)$" data-val-regex="Invalid First Name" data-val-length-max="100" data-val-length="The field FirstName must be a string with a maximum length of 100." data-val="true" class="textfield ui-input-text ui-body-d ui-corner-all ui-shadow-inset valid">
上述 html 中的正则表达式模式未按照模型的正则表达式中指定的方式呈现,这甚至会导致错误输入有效数据时(Sam's
)。
我该如何处理这个问题?
--更新--
我已经按照 @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; }
查看源代码显示以下内容:
<input data-val="true" data-val-length="The field FirstName must be a string with a maximum length of 100." data-val-length-max="100" data-val-regex="Enter only alphabets and numbers of First Name" data-val-regex-pattern="([a-zA-Z0-9 .&amp;&#39;-]+)" id="FirstName" maxlength="100" name="FirstName" type="text" value="" />
我仍然有同样的问题。
The DataAnnotations validator not working in asp.net mvc 4 razor view, when using the special characters in the regular expression.
Model:
[StringLength(100)]
[Display(Description = "First Name")]
[RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
public string FirstName { get; set; }
Razor View:
@Html.TextBoxFor(model => Model.FirstName, new { })
@Html.ValidationMessageFor(model => Model.FirstName)
The unobtrusive validation is rendered in view as:
<input type="text" value="" tabindex="1" style="height:auto;" name="FirstName" maxlength="100" id="FirstName" data-val-regex-pattern="^([a-zA-Z0-9 .&'-]+)$" data-val-regex="Invalid First Name" data-val-length-max="100" data-val-length="The field FirstName must be a string with a maximum length of 100." data-val="true" class="textfield ui-input-text ui-body-d ui-corner-all ui-shadow-inset valid">
The regex pattern in the above html is not rendered as specified in the Model's RegularExpression, which results in error even when entering the valid data (Sam's
).
How can i handle this?
--UPDATE--
I have updated the code as per @Rick suggestion
[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 following:
<input data-val="true" data-val-length="The field FirstName must be a string with a maximum length of 100." data-val-length-max="100" data-val-regex="Enter only alphabets and numbers of First Name" data-val-regex-pattern="([a-zA-Z0-9 .&'-]+)" id="FirstName" maxlength="100" name="FirstName" type="text" value="" />
Still i have the same issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
2012 年 7 月 9 日更新 - 看起来这个问题已在 RTM 中修复。
^
和$
,因此您无需添加它们。 (包含它们似乎不是问题,但您不需要它们)查看源代码显示以下内容:
看起来我们正在双重编码。
UPDATE 9 July 2012 - Looks like this is fixed in RTM.
^
and$
so you don't need to add them. (It doesn't appear to be a problem to include them, but you don't need them)View source shows the following:
It looks like we're double encoding.
尝试转义这些字符:
Try escaping those characters:
在表达式开始处尝试@符号。因此,您不需要输入转义字符,只需将正则表达式复制粘贴到“”中并输入@符号即可。就像这样:
Try @ sign at start of expression. So you wont need to type escape characters just copy paste the regular expression in "" and put @ sign. Like so:
这个对我有用,试试这个
This one worked for me, try this
您使用什么浏览器?我输入了您的示例并在 IE8 和 Chrome 中进行了尝试,当我输入值
Sam's
时,它验证正常。当我使用 IE 开发人员工具栏和 Chrome 开发人员模式检查 DOM 时,它没有显示任何特殊字符。
What browser are you using? I entered your example and tried in both IE8 and Chrome and it validated fine when I typed in the value
Sam's
When I inspect the DOM using IE Developer toolbar and Chrome Developer mode it does not show any special characters.
我们过去也遇到过类似的问题(正如 TweeZz 提到的)。
在我们的例子中,我们通过自定义 htmlHelper 扩展方法来控制 TextBoxFor 的输出,该方法正在构建 MvcHtmlString,我们需要在一个步骤中添加这些不引人注目的验证属性,这是通过
调用此方法后完成的,属性是 html 编码的,所以我们只需检查那里是否有正则表达式验证器,如果有,我们对该属性进行 html 取消编码,然后将它们合并到 tagBuilder 中(用于构建“输入”标签)
。 amps,这就是为什么这个字面替换
We've had similar issue in the past (as mentioned by TweeZz).
In our case we're controlling outputting of TextBoxFor by our custom htmlHelper extension method which is building MvcHtmlString, there in one step we need to add these unobtrusive validation attributes, which is done via
after call to this method, attributes are html encoded, so we simply check if there was Regular expression validator there and if so, we html unencode this attribute and then merge them into tagBuilder (for building "input" tag)
We only cared about & amps, that's why this literal replacement
尝试对这些值使用 ASCII 代码:
\x26
=&
\x27
='
格式为 < code>\xnn 其中 nn 是两位十六进制字符代码。您还可以使用
\unnnn
为 Unicode 字符指定四位十六进制字符代码。Try using the ASCII code for those values:
\x26
=&
\x27
='
The format is
\xnn
where nn is the two-digit hexadecimal character code. You could also use\unnnn
to specify a four-digit hex character code for the Unicode character.问题是正则表达式模式被 HTML 编码两次,一次是在构建正则表达式时,一次是在视图中呈现时。
现在,尝试将 TextBoxFor 包装在
Html.Raw
中,如下所示:The problem is that the regex pattern is being HTML encoded twice, once when the regex is being built, and once when being rendered in your view.
For now, try wrapping your TextBoxFor in an
Html.Raw
, like so:在模型课上尝试一下这个
Try this one in model class