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.

0

2 Answers 2

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)); 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.