Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows forms textbox or c# string native method for checking non-alphanumeric character

Is there a windows form textbox or c# string native member method that checks if its contents have any non-alphanumeric character?

or do I have to do it manually?

EDIT: I used @Habib's answer and added so that white spaces are checked as well, and to my surprise, it worked! lol

bool result = strVariable.Any(r=> (!char.IsLetterOrDigit(r) && !char.IsWhiteSpace(r)));

Btw, I've never used the "lambda" expression that's why I'm surprised the code above worked when I added the whitespace condition on @Habib's initial answer.

like image 873
Mikk Avatar asked Dec 29 '25 03:12

Mikk


1 Answers

You can use char.IsLetterOrDigit

Indicates whether a Unicode character is categorized as a letter or a decimal digit.

bool result = strVariable.Any(r=> !char.IsLetterOrDigit(r));
like image 120
Habib Avatar answered Dec 31 '25 18:12

Habib