1

In my model I have the following:

[Required(ErrorMessage = "First name is required.")]
[Display(Name = "First Name")]
[MaxLength(50)]
[RegularExpression(@"/^[A-z]+$/", ErrorMessage = "Only alphabet characters are allowed.")]
public string FirstName { get; set; }

In the form it's failing the check no matter what I put in there. I want it to just make sure there are only letters, no numbers or special characters.

This is not a duplicate because the referenced post has nothing to do with data annotations. They're two different contexts.

Nathan McKaskle
  • 2,926
  • 12
  • 55
  • 93

1 Answers1

2

You need to remove the regex delimiters (that are not used in .NET regex and are thus treated as literal slashes) and replace A-z with A-Za-z (see Why is this regex allowing a caret?).

Use

@"^[A-Za-z]+$"
Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563