Computer User
Computer User

Reputation: 2869

Check string contains only alphabets in C# and other allowed charaters are: ".", whitespace, ","

How do I check that string does not contain only alphabets in C#, and other allowed charaters are: dot: ".", whitespace, and comma: ","

Regex regex = new Regex("Regex Pattern");
bool result = regex.IsMatch(string);
if(result)
 Messagebox.Show("String does not contain alphabets");

Examples:

ABC1 = false

ABC = true

ABC ABC = true

abc abc = true

A. B. ABC = true

The reason is because I have a field which contains people's names. Generally in these formats:

First

First Last

First Middle Last

F. M. Last

Title. F. M. Last

So, I want to validate those using regex.

Upvotes: 0

Views: 5406

Answers (1)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73472

Try this

var regex = new Regex(@"(?i)^[a-z.,\s]+$");
bool res =  regex.IsMatch(subject);

Upvotes: 3

Related Questions