//驗證使用者輸入的字串中時候只含有數字或字母,漢字 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;//先匯入這個使用Regexprivate void isHanzi(string str) { Regex reg = new Regex(@"[u4e00-u9fa5]");//Regex 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地址");}}