Reputation: 333
I'm trying to write in if statement that runs if a string contains any non-alpahnumeric character with the exception of an underscore.
This is what I have and I'm trying to figure a simple way to add the exception for underscore but I'm having difficulty. (where key is a string).
// Check for non-alphanumerics except underscore
if (!(key.All(char.IsLetterOrDigit)))
{
validationResult = false;
}
Upvotes: 0
Views: 3239
Reputation: 239664
You just need to extend the logic within the All
:
if (!(key.All(c => char.IsLetterOrDigit(c) || c=='_')))
Upvotes: 5