user2138160
user2138160

Reputation: 279

How to validated that an object contains only alphabet characters with DataAnnotation?

I am writing some code validating that the user has submitted an object with the correct information. In other words, how would I validate that the name contains no numbers, signs, and other notation except from the English alphabet or latin foreign vowels (french, german, etc)?

    [Required]
    [MaxLength(50)]
    [DataType()] //I think a solution would use custom datatypes
    public string Name { get; set; }

Upvotes: 0

Views: 947

Answers (1)

Simon Whitehead
Simon Whitehead

Reputation: 65079

You can use Regular Expressions:

[RegularExpression(@"^[\p{L}]+$")]

This basically says "only allow any Unicode character in the entire thing".

Upvotes: 3

Related Questions