文章分類

C# (27) ASP.NET (13) SQL (7) ORACLE (5) JAVA (2) SQLSERVER2008 (2) 大家都在問的事 (2) ACCESS (1) ANDRIOD (1) JQUERY (1) python (1) 雜談 (1)

關於我自己

我的相片
程式初心者 JAVA, ASP.NET, C# ,SQL

2011年2月7日 星期一

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



private void TextBox1_KeyPress(object sender, KeyPressEventArgs e) //文本框输入字符过滤
{

if (Convert.ToInt32(e.KeyChar) >= 48 && Convert.ToInt32(e.KeyChar) < style="word-break: break-all; line-height: normal !important; "> //(Convert.ToInt32(e.KeyChar) >= 65 && Convert.ToInt32(e.KeyChar) < style="word-break: break-all; line-height: normal !important; "> //(Convert.ToInt32(e.KeyChar) >= 97 && Convert.ToInt32(e.KeyChar) < style="word-break: break-all; line-height: normal !important; "> //(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 < style="word-break: break-all; line-height: normal !important; "> {
if ((Convert.ToInt32(buf[k]) >= 48 && Convert.ToInt32(buf[k]) < style="word-break: break-all; line-height: normal !important; "> //(Convert.ToInt32(buf[k]) >= 65 && Convert.ToInt32(buf[k]) < style="word-break: break-all; line-height: normal !important; "> //(Convert.ToInt32(buf[k]) >= 97 && Convert.ToInt32(buf[k]) < style="word-break: break-all; line-height: normal !important; "> //(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;
}