Verifying that a string contains only letters in C#
Only letters:
Regex.IsMatch(input, @"^[a-zA-Z]+$");
Only letters and numbers:
Regex.IsMatch(input, @"^[a-zA-Z0-9]+$");
Only letters, numbers and underscore:
Regex.IsMatch(input, @"^[a-zA-Z0-9_]+$");
bool result = input.All(Char.IsLetter);
bool result = input.All(Char.IsLetterOrDigit);
bool result = input.All(c=>Char.IsLetterOrDigit(c) || c=='_');
Letters only:
Regex.IsMatch(theString, @"^[\p{L}]+$");
Letters and numbers:
Regex.IsMatch(theString, @"^[\p{L}\p{N}]+$");
Letters, numbers and underscore:
Regex.IsMatch(theString, @"^[\w]+$");
Note, these patterns also match international characters (as opposed to using the a-z
construct).