8

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.

Phil Hale
  • 3,453
  • 2
  • 36
  • 50

2 Answers2

1

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.

Community
  • 1
  • 1
Phil Hale
  • 3,453
  • 2
  • 36
  • 50
0

Try this:

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

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

Cylian
  • 10,970
  • 4
  • 42
  • 55