ASP.NET MVC4正则表达式模型验证属性不适用于带有重音字符的客户端

时间:2020-12-13 16:34:20

In an ASP.NET MVC 3 project I have a requirement to validate a name field in a view to allow a specific set accented characters. So in my view model I have an regular expression attribute defined on the appropriate property like this:

在ASP.NET MVC 3项目中,我需要验证视图中的名称字段以允许特定的集合重音字符。所以在我的视图模型中,我在相应的属性上定义了一个正则表达式属性,如下所示:

[RegularExpression("^[a-zA-Zá]{2,50}$")]

Please note this is not the exact code, it is simplified to make my problem easier to understand.

请注意,这不是确切的代码,它简化了以使我的问题更容易理解。

This regular expression works fine server side, but doesn't work client side. If you view the HTML of the input field is contains this attribute:

这个正则表达式在服务器端工作正常,但在客户端不起作用。如果查看输入字段的HTML包含此属性:

data-val-regex-pattern="^[a-zA-Zá]{2,50}$"

As you can see the accented character has been converted into a HTML entity which breaks the regular expression. Can anyone tell me why this is happening and how to fix it?

如您所见,重音字符已转换为HTML实体,该实体会破坏正则表达式。任何人都可以告诉我为什么会这样,以及如何解决它?

UPDATE

UPDATE

Apologies I am a complete moron. I had completely forgotten that we upgraded to MVC 4 beta a couple of days ago. Subsequently I've created a two small test projects, one in MVC 3 and one in MVC 4. The problem only exists in MVC 4.

道歉我是一个完整的白痴。我完全忘记了几天前我们升级到MVC 4测试版。随后我创建了两个小型测试项目,一个在MVC 3中,另一个在MVC 4中。这个问题只存在于MVC 4中。

2 个解决方案

#1


1  

Turns out someone has asked the same question. My Google searches didn't find it until now.

事实证明有人问了同样的问题。我的Google搜索直到现在才找到它。

DataAnnotations validation (Regular Expression) in asp.net mvc 4 - razor view

asp.net mvc 4中的DataAnnotations验证(正则表达式) - razor视图

The problem has been reported as a bug in MVC 4 beta.

该问题已被报道为MVC 4 beta中的一个错误。

#2


0  

Try this:

尝试这个:

^[a-zA-Z\u00E1]{2,50}$

Using \uXXXX, where XXXX hex code of the character.

使用\ uXXXX,其中XXXX十六进制代码的字符。

#1


1  

Turns out someone has asked the same question. My Google searches didn't find it until now.

事实证明有人问了同样的问题。我的Google搜索直到现在才找到它。

DataAnnotations validation (Regular Expression) in asp.net mvc 4 - razor view

asp.net mvc 4中的DataAnnotations验证(正则表达式) - razor视图

The problem has been reported as a bug in MVC 4 beta.

该问题已被报道为MVC 4 beta中的一个错误。

#2


0  

Try this:

尝试这个:

^[a-zA-Z\u00E1]{2,50}$

Using \uXXXX, where XXXX hex code of the character.

使用\ uXXXX,其中XXXX十六进制代码的字符。