Verifying that a string contains only letters in C#
I have an input string and I want to verify that it contains:
- Only letters or
- Only letters and numbers or
- Only letters, numbers or underscore
To clarify, I have 3 different cases in the code, each calling for different validation. What's the simplest way to achieve this 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=='_');