C中#如何对textBox输入的数据进行数据检验
来源:百度知道 编辑:UC知道 时间:2024/05/21 12:00:11
1检验输入的是否全部为数字
2检验输入的是否全部为字符串
谢谢大家了!
用正则表达式
用异常? -_-!
private void textBox1_TextChanged(object sender, EventArgs e) {
string str = this.textBox1.Text;
bool isNum = true;
foreach (char ch in str) {
if (ch < 48 || ch > 57)
isNum = false;
}
}
这个是JS的。
//数字判断函数,返回true表示是全部数字,返回false表示不全部是数字
function isNumber(str)
{
if(""==str)
{
return false;
}
var reg = /\D/;
return str.match(reg)==null;
}
<input id="Text1" onkeyup="txt_change()">//这个是你要验证的控件,每输入一个字符就验证一次。确保每个输入都是数字。
string text=this.textBox1.Text;
int digit=0;//纪录数字个数
int letter=0;//纪录字母个数
for(int i=0;i<text.Length;i++)
{
if(char.IsDigit((char)text[i]))
{digit++;}
if(char.IsLetter((char)text[i])){
letter++;}
}