检测输入字符串仅由数字或字母组成 [英] Detecting that the input string just consist of digits or letters

查看:81
本文介绍了检测输入字符串仅由数字或字母组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在我的程序中,有一个文本框,要求用户为自己创建一个用户名.用户名只能由数字或字母组成.我的意思是@,&,#或...应该排除在外.如何检测输入的用户名仅包含这些合法(数字或字母)条目.

非常感谢

Hello,

In my program, there is a textbox asking the user to create a Username for himself. The Username should consist of digits or letters only. I mean, @, &, # or ... should be excluded. How can I detect that the entered Username just includes these legal (digits or letters) entries.

Thanks a lot

推荐答案

一种使用正则表达式的方法...

one way to do that is using regular expressions ...

Regex regex = new Regex("^[A-Za-z0-9]+


"); if (regex.IsMatch(textBox1.Text)) MessageLabel.Text= "valid"; else MessageLabel.Text= "Not Valid";
"); if (regex.IsMatch(textBox1.Text)) MessageLabel.Text= "valid"; else MessageLabel.Text= "Not Valid";



就在我的头顶...



just off the top of my head ...


如果您不想使用Regex,可以遍历字符串中的每个字符并使用
If you don''t want to use Regex you can loop through each character in the string and test it with char.IsLetterOrDigit[^]


这篇关于检测输入字符串仅由数字或字母组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆