The DataAnnotations validator not working in asp.net mvc 4 razor view, when using the special characters in the regular expression.
当使用正则表达式中的特殊字符时,dataannotation确认器不能在asp.net mvc 4 razor视图中工作。
Model:
模型:
[StringLength(100)]
[Display(Description = "First Name")]
[RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
public string FirstName { get; set; }
Razor View:
Razor视图:
@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 .&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">
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
).
上面html中的regex模式没有按照模型的正则表达式中指定的那样呈现,即使在输入有效数据(Sam's)时也会导致错误。
How can i handle this?
我怎么处理这件事?
--UPDATE--
——更新
I have updated the code as per @Rick suggestion
我已经按照@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 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 .&amp;&#39;-]+)" id="FirstName" maxlength="100" name="FirstName" type="text" value="" />
Still i have the same issue.
我还是有同样的问题。
8 个解决方案
#1
33
UPDATE 9 July 2012 - Looks like this is fixed in RTM.
更新2012年7月9日-看起来这是在RTM中修复的。
- We already imply
^
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) - 我们已经暗示^和$所以你不需要添加它们。(把它们包括进来似乎不是问题,但您不需要它们)
- This appears to be a bug in ASP.NET MVC 4/Preview/Beta. I've opened a bug
- 这似乎是ASP中的一个错误。净MVC 4 /预览/β。我开了一个错误
View source shows the following:
查看源显示以下内容:
data-val-regex-pattern="([a-zA-Z0-9 .&'-]+)" <-- MVC 3
data-val-regex-pattern="([a-zA-Z0-9 .&amp;&#39;-]+)" <-- MVC 4/Beta
It looks like we're double encoding.
看起来我们是双重编码。
#2
9
Try escaping those characters:
试着逃避这些字符:
[RegularExpression(@"^([a-zA-Z0-9 \.\&\'\-]+)$", ErrorMessage = "Invalid First Name")]
#3
5
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:
在表达式开始时尝试@符号。因此,您不需要输入转义字符,只需复制“”中的正则表达式并加上@符号。像这样:
[RegularExpression(@"([a-zA-Z\d]+[\w\d]*|)[a-zA-Z]+[\w\d.]*", ErrorMessage = "Invalid username")]
public string Username { get; set; }
#4
1
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
你在用什么浏览器?我输入了您的示例并在IE8和Chrome中进行了尝试,当我输入Sam的值时,它得到了很好的验证
public class IndexViewModel
{
[Required(ErrorMessage="Required")]
[RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
public string Name { get; set; }
}
When I inspect the DOM using IE Developer toolbar and Chrome Developer mode it does not show any special characters.
当我使用IE Developer工具栏和Chrome Developer模式检查DOM时,它没有显示任何特殊字符。
#5
1
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
我们过去也遇到过类似的问题(如TweeZz提到的)。在我们的例子中,我们通过我们的自定义htmlHelper扩展方法控制TextBoxFor的输出,该方法正在构建MvcHtmlString,在一步中,我们需要添加这些不显眼的验证属性,这是通过
var attrs = htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata)
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)
在调用此方法之后,属性是html编码的,因此我们只需检查是否存在正则表达式验证器,如果有,我们html将这个属性解压,然后将它们合并到tagBuilder中(用于构建“输入”标记)
if(attrs.ContainsKey("data-val-regex"))
attrs["data-val-regex"] = ((string)attrs["data-val-regex"]).Replace("&","&");
tagBuilder.MergeAttributes(attrs);
We only cared about & amps, that's why this literal replacement
我们只关心& amps,这就是为什么这个字面替换
#6
0
Try using the ASCII code for those values:
请尝试使用ASCII码进行这些值:
^([a-zA-Z0-9 .\x26\x27-]+)$
-
\x26
=&
- \ x26 = &
-
\x27
='
- \ 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.
格式是\xnn,其中nn是两位数的十六进制字符代码。您还可以使用\unnnn为Unicode字符指定四位十六进制字符代码。
#7
0
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.
问题是regex模式是HTML编码的两次,一次是在构建regex时,一次是在您的视图中呈现。
For now, try wrapping your TextBoxFor in an Html.Raw
, like so:
现在,尝试用Html包装你的文本框。生,就像这样:
@Html.Raw(Html.TextBoxFor(model => Model.FirstName, new { }))
#8
0
Try this one in model class
在模型类中试试这个。
[Required(ErrorMessage = "Enter full name.")]
[RegularExpression("([A-Za-z])+( [A-Za-z]+)", ErrorMessage = "Enter valid full name.")]
public string FullName { get; set; }
#1
33
UPDATE 9 July 2012 - Looks like this is fixed in RTM.
更新2012年7月9日-看起来这是在RTM中修复的。
- We already imply
^
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) - 我们已经暗示^和$所以你不需要添加它们。(把它们包括进来似乎不是问题,但您不需要它们)
- This appears to be a bug in ASP.NET MVC 4/Preview/Beta. I've opened a bug
- 这似乎是ASP中的一个错误。净MVC 4 /预览/β。我开了一个错误
View source shows the following:
查看源显示以下内容:
data-val-regex-pattern="([a-zA-Z0-9 .&'-]+)" <-- MVC 3
data-val-regex-pattern="([a-zA-Z0-9 .&amp;&#39;-]+)" <-- MVC 4/Beta
It looks like we're double encoding.
看起来我们是双重编码。
#2
9
Try escaping those characters:
试着逃避这些字符:
[RegularExpression(@"^([a-zA-Z0-9 \.\&\'\-]+)$", ErrorMessage = "Invalid First Name")]
#3
5
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:
在表达式开始时尝试@符号。因此,您不需要输入转义字符,只需复制“”中的正则表达式并加上@符号。像这样:
[RegularExpression(@"([a-zA-Z\d]+[\w\d]*|)[a-zA-Z]+[\w\d.]*", ErrorMessage = "Invalid username")]
public string Username { get; set; }
#4
1
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
你在用什么浏览器?我输入了您的示例并在IE8和Chrome中进行了尝试,当我输入Sam的值时,它得到了很好的验证
public class IndexViewModel
{
[Required(ErrorMessage="Required")]
[RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
public string Name { get; set; }
}
When I inspect the DOM using IE Developer toolbar and Chrome Developer mode it does not show any special characters.
当我使用IE Developer工具栏和Chrome Developer模式检查DOM时,它没有显示任何特殊字符。
#5
1
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
我们过去也遇到过类似的问题(如TweeZz提到的)。在我们的例子中,我们通过我们的自定义htmlHelper扩展方法控制TextBoxFor的输出,该方法正在构建MvcHtmlString,在一步中,我们需要添加这些不显眼的验证属性,这是通过
var attrs = htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata)
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)
在调用此方法之后,属性是html编码的,因此我们只需检查是否存在正则表达式验证器,如果有,我们html将这个属性解压,然后将它们合并到tagBuilder中(用于构建“输入”标记)
if(attrs.ContainsKey("data-val-regex"))
attrs["data-val-regex"] = ((string)attrs["data-val-regex"]).Replace("&","&");
tagBuilder.MergeAttributes(attrs);
We only cared about & amps, that's why this literal replacement
我们只关心& amps,这就是为什么这个字面替换
#6
0
Try using the ASCII code for those values:
请尝试使用ASCII码进行这些值:
^([a-zA-Z0-9 .\x26\x27-]+)$
-
\x26
=&
- \ x26 = &
-
\x27
='
- \ 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.
格式是\xnn,其中nn是两位数的十六进制字符代码。您还可以使用\unnnn为Unicode字符指定四位十六进制字符代码。
#7
0
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.
问题是regex模式是HTML编码的两次,一次是在构建regex时,一次是在您的视图中呈现。
For now, try wrapping your TextBoxFor in an Html.Raw
, like so:
现在,尝试用Html包装你的文本框。生,就像这样:
@Html.Raw(Html.TextBoxFor(model => Model.FirstName, new { }))
#8
0
Try this one in model class
在模型类中试试这个。
[Required(ErrorMessage = "Enter full name.")]
[RegularExpression("([A-Za-z])+( [A-Za-z]+)", ErrorMessage = "Enter valid full name.")]
public string FullName { get; set; }