Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net regular expression integration

Tags:

c#

regex

asp.net

enter image description hereI 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")]
like image 511
Uddyan Semwal Avatar asked Feb 25 '19 11:02

Uddyan Semwal


2 Answers

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

like image 91
Prasad Telkikar Avatar answered Oct 19 '22 14:10

Prasad Telkikar


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

like image 1
ElasticCode Avatar answered Oct 19 '22 16:10

ElasticCode