一.網絡驗證應用技巧
1. 驗證 E-mail格式
public bool IsEmail(string str_Email) { return System.Text.RegularExpressions.Regex.IsMatch(str_Email,@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); }
2. 驗證 IP 地址
public bool IPCheck(string IP) { string num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)"; return Regex.IsMatch(IP,("^" + num + "\\." + num + "\\." + num + "\\." + num + "$")); }
3. 驗證 URL
public bool IsUrl(string str_url) { return System.Text.RegularExpressions.Regex.IsMatch(str_url, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"); }
二. 常用數字驗證技巧
1. 驗證電話號碼
public bool IsTelephone(string str_telephone) { return System.Text.RegularExpressions.Regex.IsMatch(str_telephone, @"^(\d{3,4}-)?\d{6,8}$"); }
2. 輸入密碼條件(字符與數據同時出現)
public bool IsPassword(string str_password) { return System.Text.RegularExpressions.Regex.IsMatch(str_password, @"[A-Za-z]+[0-9]"); }
3. 郵政編號
public bool IsPostalcode(string str_postalcode) { return System.Text.RegularExpressions.Regex.IsMatch(str_postalcode, @"^\d{6}$"); }
4. 手機號碼
public bool IsHandset(string str_handset) { return System.Text.RegularExpressions.Regex.IsMatch(str_handset, @"^[1]+[3,5]+\d{9}$"); }
5. 身份證號
public bool IsIDcard(string str_idcard) { return System.Text.RegularExpressions.Regex.IsMatch(str_idcard, @"(^\d{18}$)|(^\d{15}$)"); }
6. 兩位小數
public bool IsDecimal(string str_decimal) { return System.Text.RegularExpressions.Regex.IsMatch(str_decimal, @"^[0-9]+(.[0-9]{2})?$"); }
7. 一年的12個月
public bool IsMonth(string str_Month) { return System.Text.RegularExpressions.Regex.IsMatch(str_Month, @"^(0?[[1-9]|1[0-2])$"); }
8. 一個月的31天
public bool IsDay(string str_day) { return System.Text.RegularExpressions.Regex.IsMatch(str_day, @"^((0?[1-9])|((1|2)[0-9])|30|31)$"); }
9. 數字輸入
public bool IsNumber(string str_number) { return System.Text.RegularExpressions.Regex.IsMatch(str_number, @"^[0-9]*$"); } 10. 密碼長度 (6-18位)
public bool IsPasswLength(string str_Length) { return System.Text.RegularExpressions.Regex.IsMatch(str_Length, @"^\d{6,18}$"); }
11. 非零的正整數
public bool IsIntNumber(string str_intNumber) { return System.Text.RegularExpressions.Regex.IsMatch(str_intNumber, @"^\+?[1-9][0-9]*$"); }
三. 常用字符驗證技巧
1. 大寫字母
public bool IsUpChar(string str_UpChar) { return System.Text.RegularExpressions.Regex.IsMatch(str_UpChar, @"^[A-Z]+$"); }
2. 小寫字母
public bool IsLowChar(string str_UpChar) { return System.Text.RegularExpressions.Regex.IsMatch(str_UpChar, @"^[a-z]+$"); }
3. 檢查字符串重復出現的詞
private void btnWord_Click(object sender, EventArgs e) { System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(label1.Text, @"\b(?<word>\w+)\s+(\k<word>)\b", System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase); if (matches.Count != 0) { foreach (System.Text.RegularExpressions.Match match in matches) { string word = match.Groups["word"].Value; MessageBox.Show(word.ToString(),"英文單詞"); } } else { MessageBox.Show("沒有重復的單詞"); } }
4. 替換字符串
private void button1_Click(object sender, EventArgs e) { string strResult = System.Text.RegularExpressions.Regex.Replace(textBox1.Text, @"[A-Za-z]\*?", textBox2.Text); MessageBox.Show("替換前字符:" + "\n" + textBox1.Text + "\n" + "替換的字符:" + "\n" + textBox2.Text + "\n" + "替換后的字符:" + "\n" + strResult,"替換"); }
5. 拆分字符串
private void button1_Click(object sender, EventArgs e) { //實例: 甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁 foreach (string s in System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"\d{3,4}-\d*")) { textBox2.Text+=s; //依次輸出 "甲乙丙丁" } }
6. 驗證輸入字母
public bool IsLetter(string str_Letter) { return System.Text.RegularExpressions.Regex.IsMatch(str_Letter, @"^[A-Za-z]+$"); }
7. 驗證輸入漢字
public bool IsChinese(string str_chinese) { return System.Text.RegularExpressions.Regex.IsMatch(str_chinese, @"^[\u4e00-\u9fa5],{0,}$"); }
8. 驗證輸入字符串 (至少8個字符)
public bool IsLength(string str_Length) { return System.Text.RegularExpressions.Regex.IsMatch(str_Length, @"^.{8,}$"); }