您的位置:首页 > 编程语言 > C#

C# 字符、字符串过滤,只能输入数字、中文、英文、大写、小写

2009-11-10 13:17 721 查看
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e) //文本框输入字符过滤
{

if (Convert.ToInt32(e.KeyChar) >= 48 && Convert.ToInt32(e.KeyChar) < 58 || //包括数字
//(Convert.ToInt32(e.KeyChar) >= 65 && Convert.ToInt32(e.KeyChar) < 91) || //包括大写字母
//(Convert.ToInt32(e.KeyChar) >= 97 && Convert.ToInt32(e.KeyChar) < 123) || //包括小写字母
//(Convert.ToInt32(e.KeyChar) == 46) || //包括.
//(Convert.ToInt32(e.KeyChar) == 32) || //包括空格
//(Convert.ToInt32(e.KeyChar) == 64) || //包括@
//(Convert.ToInt32(e.KeyChar) > 127) || //包括中文
Convert.ToInt32(e.KeyChar) == 8) //包括退格
{

}
else
{
e.Handled = true;
}

public static bool ReplaceCodes(String Temp) //检查字符串过滤方法
{

string s = Temp;
int l = s.Length;
char[] buf = s.ToCharArray();
string tmp = string.Empty;
for (int k = 0; k < buf.Length; k++)
{
if ((Convert.ToInt32(buf[k]) >= 48 && Convert.ToInt32(buf[k]) < 58))//|| //数字
//(Convert.ToInt32(buf[k]) >= 65 && Convert.ToInt32(buf[k]) < 91) || //大写字母
//(Convert.ToInt32(buf[k]) >= 97 && Convert.ToInt32(buf[k]) < 123) || //小写字母
//(Convert.ToInt32(buf[k]) == 46) || //.
//(Convert.ToInt32(buf[k]) == 32) || //空格
//(Convert.ToInt32(buf[k]) == 64) || //@
//(Convert.ToInt32(buf[k]) > 127) || //中文
//(Convert.ToInt32(buf[k]) == 8)) //退格
{
tmp += buf[k].ToString();

}
}
if (tmp.Length != l)
{
// Util.info("请输入正确整数数字!");
//Temp = tmp;
return false;
}
else
return true;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐