I am trying to create a regular expression in C# that allows only alphabet characters and spaces. i just tried this.
[Required(ErrorMessage = "Please Enter Name")]
[Display(Name = "Name")]
[RegularExpression("^([a-zA-Z])",
ErrorMessage = "Please Enter Correct Name")]
You can try
[RegularExpression("^([A-Za-z ]+$)",
ErrorMessage = "Please Enter Correct Name")]
Description
^
- Beginning of string
[ ]
- Brackets specifies set of characters
A-za-z
- All capital/small letters
- Consider a space
+
- one or more letters
$
- Indicates end of string
You can use [A-Za-z\s]+
it will matches alphabet characters and spaces
[RegularExpression("[A-Za-z\s]+", ErrorMessage = "Please Enter Correct Name")]
\s
matches any whitespace character
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With