RegularExpressionAttribute javascript编码

时间:2012-09-25 12:16:18

标签: regex asp.net-mvc-3 attributes custom-attributes

假设您在ViewModel中有一个属性名称:

[Required(ErrorMessage = "{0} is required.")]       
[RegularExpressionAttribute("[a-zA-Z äöåÄÖÅ]+", ErrorMessage = "Field {0} must contain only letters.")]      
public string Name{ get; set; }  

在某些浏览器中,这可以正常运行,因为它没有被编码:

<input class="name valid" data-val="true" data-val-regex="Field Name must contain only letters." data-val-regex-pattern="[a-zA-Z äöåÄÖÅ]+" data-val-required="Name is required." name="Name" type="text" value="">

但在某些浏览器中,这会被编码,因此正则表达式不起作用

<input class="name valid" data-val="true" data-val-regex="Field Name must contain only letters." data-val-regex-pattern="[a-zA-Z &amp;#228;&amp;#246;&amp;#229;&amp;#196;&amp;#214;&amp;#197;]+" data-val-required="Name is required." name="Name" type="text" value="">

我调查过并发现javascript存在问题 https://stackoverflow.com/a/4044768/1241400

有没有解决方法?

1 个答案:

答案 0 :(得分:1)

你可以尝试Unicode转义:

"[A-Za-z \\u00E4\\u00F6\\u00E5\\u00C4\\u00D6\\u00C5]+"