日期:2014-05-18 浏览次数:20845 次
//验证用户输入的字符串中时候只含有数字或字母,汉字 bool IsValidString(string strIn) { return Regex.IsMatch(strIn,@"^[A-Za-z0-9\u4e00-\u9fa5]+$"); } //验证电子邮件 bool IsValidEmail(string strIn) { return Regex.IsMatch(strIn,@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } //验证用户输入的字符串中时候只含有数字或字母 bool IsValidString(string strIn) { return Regex.IsMatch(strIn,@"^[A-Za-z0-9]+$"); } /// <summary> /// 判断密码 /// </summary> /// <param name="PassWord"></param> /// <returns></returns> public static bool IsValidPassWord(string PassWord) { return Regex.IsMatch(PassWord, @"^(\w){6,20}$"); } /// <summary> /// 判断电话/传真 /// </summary> /// <param name="Tel"></param> /// <returns></returns> public static bool IsValidTel(string Tel) { return Regex.IsMatch(Tel, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$"); } /// <summary> /// 判断手机 /// </summary> /// <param name="Mobil"></param> /// <returns></returns> public static bool IsValidMobil(string Mobil) { return Regex.IsMatch(Mobil, @"^(\d)+[-]?(\d){6,12}$"); } /// <summary> /// 邮政编码 /// </summary> /// <param name="Zip"></param> /// <returns></returns> public static bool IsValidZip(string Zip) { return Regex.IsMatch(Zip, @"^[a-z0-9 ]{3,12}$"); } /// <summary> /// 判断日期 /// </summary> /// <param name="Date"></param> /// <returns></returns> public static bool IsValidDate(string Date) { bool bValid=Regex.IsMatch(Date, @"^[12]{1}(\d){3}[-][01]?(\d){1}[-][0123]?(\d){1}$"); return (bValid && Date.CompareTo("1753-01-01")>=0); } //判断整型 /*public static bool IsValidInt(string Int) { return Regex.IsMatch(Int, @"^[1-9]{1}[0-9]{0,6}$"); }*/ /// <summary> /// 判断只能输字母 /// </summary> /// <param name="EnName"></param> /// <returns></returns> public static bool IsValidEnName(string EnName) { return Regex.IsMatch(EnName, @"[a-zA-Z]"); } C#判断字符串中是否有汉字 using System.Text.RegularExpressions;//先导入这个使用正则表达式 private void isHanzi(string str) { Regex reg = new Regex(@"[u4e00-u9fa5]");//正则表达式 if (reg.IsMatch(str)) { MessageBox.Show("有汉字"); } else { MessageBox.Show("没有汉字"); } } C#验证是否是合法邮箱 先 using System.Text.RegularExpressions; 然后按钮中加入 private void button1_Click(object sender, EventArgs e) { string reg = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"; Regex r = new Regex(reg); if (r.IsMatch(textBox1.Text)) { MessageBox.Show("格式正确"); } else { MessageBox.Show("不是有效的email地址"); } }