C#如何检查文本框是否包含数字? [英] C# how to check if textbox contains numbers?

查看:77
本文介绍了C#如何检查文本框是否包含数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我有文本框名称,当然我不能在其中写入数字,因为名称不能是数字,我该怎么检查呢?



我的尝试:



for example i have textbox Name, and of course i can't write in it a number, because the name can't be number,how can i check it?

What I have tried:

string alph = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
            if (!textBox1.Text.Contains(alph))
            {
                
                MessageBox.Show("This field only for letters", "Name");
                
            }

推荐答案

首先,名字可以包含任何内容:John Henderson-Allan是有效的名称。 StopFortnumAndMasonFoieGras Cruelty.com和Brfxxccxxmnpcccclllmmnprxvclmnckssqlbb11116也是如此 - 所以你不应该特别禁止任何名称,不应该禁止任何这些!#
First off, names can contain anything: "John Henderson-Allan" is a valid name. So are "StopFortnumAndMasonFoieGras Cruelty.com" and "Brfxxccxxmnpcccclllmmnprxvclmnckssqlbb11116" - so you shouldn't ban anything in particular from a name, any more than you should ban any of these "!#


% &'* + - / =?^ _`{|}〜来自电子邮件地址。



但是,要将文本框限制为大写和小写字母只使用正则表达式:

%&'*+-/=?^_`{|}~" from an email address.

But, to restrict a textbox to upper and lower case letters only just use a Regex:
Regex regex = new Regex("^[a-zA-Z]+


);
bool hasOnlyAlpha = regex.IsMatch(myTextBox.Text);
"); bool hasOnlyAlpha = regex.IsMatch(myTextBox.Text);


这篇关于C#如何检查文本框是否包含数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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