DataAnnotations validation (Regular Expression) in asp.net mvc 4 - razor view
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; }
Try escaping those characters:
[RegularExpression(@"^([a-zA-Z0-9 \.\&\'\-]+)$", ErrorMessage = "Invalid First Name")]
UPDATE 9 July 2012 - Looks like this is fixed in 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
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.