I have model validation in my ASP.NET MVC2 project, and I want to implement validation on field containing last name of some person. This field can contain any native characters (just like Ś Ć Ó and o开发者_开发技巧thers, from any language), but not special characters ( like &#$% ) and numbers.
Part of code, where I have to put it, looks like this:
[RegularExpression("SOME REGULAR EXPRESSION", ErrorMessage = "Field invalid")]
[Required(ErrorMessage = "Field required")]
Can someone help me with it?
Use a negative character group, such as [^characters-here]
. The ^
indicates that the character group is negative rather than positive. The pattern should be similar to this:
"^[^0-9!@#$%^&*-]+$"
If you decide to ignore a dash character, place it at the beginning or end of the character class to avoid incorrectly specifying a range of characters. In the pattern above I've placed it at the end. Placing it at the beginning would look like [^-...]
.
精彩评论