3

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.

Mikk
  • 455
  • 1
  • 13
  • 19

2 Answers2

4

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));
Habib
  • 219,104
  • 29
  • 407
  • 436
0

You can create a control which has TextBox as its base, but currently there are no properties which does this for you, you have to do this on the KeyUp event of the textBox and use a regex or similar

CR41G14
  • 5,464
  • 5
  • 43
  • 64