简体   繁体   中英

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.

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));

您可以创建一个以TextBox为基础的控件,但是目前没有为您执行此操作的属性,您必须在textBox的KeyUp事件上执行此操作并使用正则表达式或类似的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM