Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC4 regular expression model validation attribute doesn't work client side with accented characters

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:

[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:

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?

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.

like image 274
Phil Hale Avatar asked May 10 '12 14:05

Phil Hale


1 Answers

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

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

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

like image 80
Phil Hale Avatar answered Sep 18 '22 16:09

Phil Hale