C#驗證類(使用正則表達式)

 1None.gifusing System;
 2None.gifusing System.Text.RegularExpressions;
 3None.gif
 4None.gifnamespace bobomousecom.crm
 5ExpandedBlockStart.gif{
 6ExpandedSubBlockStart.gif /**//// <summary>
 7InBlock.gif /// Regexlib 的摘要說明。
 8ExpandedSubBlockEnd.gif /// </summary>

 9InBlock.gif public class Regexlib
10ExpandedSubBlockStart.gif {
11InBlock.gif  public Regexlib()
12ExpandedSubBlockStart.gif  {
13InBlock.gif   //
14InBlock.gif   // TODO: 在此處添加構造函數邏輯
15InBlock.gif   //
16ExpandedSubBlockEnd.gif  }

17InBlock.gif
18InBlock.gif
19InBlock.gif  //搜索輸入字符串並返回所有 href=“”值
20InBlock.gif  string DumpHrefs(String inputString) 
21ExpandedSubBlockStart.gif  {
22InBlock.gif   Regex r;
23InBlock.gif   Match m;
24InBlock.gif
25InBlock.gif   r = new Regex("href/s*=/s*(?:"(?<1>[^"]*)"|(?<1>/S+))",
26InBlock.gif    RegexOptions.IgnoreCase|RegexOptions.Compiled);
27InBlock.gif   for (m = r.Match(inputString); m.Success; m = m.NextMatch()) 
28ExpandedSubBlockStart.gif   {
29InBlock.gif    return("Found href " + m.Groups[1]);
30ExpandedSubBlockEnd.gif   }

31ExpandedSubBlockEnd.gif  }

32InBlock.gif
33InBlock.gif
34InBlock.gif
35InBlock.gif  //驗證Email地址
36InBlock.gif  bool IsValidEmail(string strIn)
37ExpandedSubBlockStart.gif  {
38InBlock.gif   // Return true if strIn is in valid e-mail format.
39InBlock.gif   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})(]?)$"); 
40ExpandedSubBlockEnd.gif  }

41InBlock.gif
42InBlock.gif
43InBlock.gif  //dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。
44InBlock.gif  string MDYToDMY(String input) 
45ExpandedSubBlockStart.gif  {
46InBlock.gif   return Regex.Replace(input,"/b(?/d{1,2})/(?/d{1,2})/(?/d{2,4})/b","${day}-${month}-${year}");
47ExpandedSubBlockEnd.gif  }

48InBlock.gif
49InBlock.gif
50InBlock.gif  //驗證是否爲小數
51InBlock.gif  bool IsValidDecimal(string strIn)
52ExpandedSubBlockStart.gif  {
53InBlock.gif   
54InBlock.gif   return Regex.IsMatch(strIn,@"[0].d{1,2}|[1]"); 
55ExpandedSubBlockEnd.gif  }

56InBlock.gif
57InBlock.gif
58InBlock.gif  //驗證是否爲電話號碼
59InBlock.gif  bool IsValidTel(string strIn)
60ExpandedSubBlockStart.gif  {
61InBlock.gif   return Regex.IsMatch(strIn,@"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?"); 
62ExpandedSubBlockEnd.gif  }

63InBlock.gif
64InBlock.gif
65InBlock.gif  //驗證年月日
66InBlock.gif  bool IsValidDate(string strIn)
67ExpandedSubBlockStart.gif  {
68InBlock.gif   return Regex.IsMatch(strIn,@"^2d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]d|3[0-1])(?:0?[1-9]|1d|2[0-3]):(?:0?[1-9]|[1-5]d):(?:0?[1-9]|[1-5]d)$"); 
69ExpandedSubBlockEnd.gif  }

70InBlock.gif
71InBlock.gif
72InBlock.gif  //驗證後綴名
73InBlock.gif  bool IsValidPostfix(string strIn)
74ExpandedSubBlockStart.gif  {
75InBlock.gif   return Regex.IsMatch(strIn,@".(?i:gif|jpg)$"); 
76ExpandedSubBlockEnd.gif  }

77InBlock.gif
78InBlock.gif
79InBlock.gif  //驗證字符是否在4至12之間
80InBlock.gif  bool IsValidByte(string strIn)
81ExpandedSubBlockStart.gif  {
82InBlock.gif   return Regex.IsMatch(strIn,@"^[a-z]{4,12}$"); 
83ExpandedSubBlockEnd.gif  }

84InBlock.gif
85InBlock.gif
86InBlock.gif  //驗證IP
87InBlock.gif  bool IsValidIp(string strIn)
88ExpandedSubBlockStart.gif  {
89InBlock.gif   return Regex.IsMatch(strIn,@"^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$"); 
90ExpandedSubBlockEnd.gif  }
 
91ExpandedSubBlockEnd.gif }

92ExpandedBlockEnd.gif}

93None.gif

None.gifC#正則表達式小結    
None.gif只能輸入數字:
"^[0-9]*$"
None.gif只能輸入n位的數字:
"^/d{n}$"
None.gif只能輸入至少n位的數字:
"^/d{n,}$"
None.gif只能輸入m
~n位的數字:。"^/d{m,n}$"
None.gif只能輸入零和非零開頭的數字:
"^(0|[1-9][0-9]*)$"
None.gif只能輸入有兩位小數的正實數:
"^[0-9]+(.[0-9]{2})?$"
None.gif只能輸入有1
~3位小數的正實數:"^[0-9]+(.[0-9]{1,3})?$"
None.gif只能輸入非零的正整數:
"^/+?[1-9][0-9]*$"
None.gif只能輸入非零的負整數:
"^/-[1-9][]0-9"*$。
None.gif只能輸入長度爲3的字符:
"^.{3}$"
None.gif只能輸入由26個英文字母組成的字符串:
"^[A-Za-z]+$"
None.gif只能輸入由26個大寫英文字母組成的字符串:
"^[A-Z]+$"
None.gif只能輸入由26個小寫英文字母組成的字符串:
"^[a-z]+$"
None.gif只能輸入由數字和26個英文字母組成的字符串:
"^[A-Za-z0-9]+$"
None.gif只能輸入由數字、26個英文字母或者下劃線組成的字符串:
"^/w+$"
None.gif只能輸入由數字、26個英文字母或者下劃線,中文組成的字符串:
^//w+$
None.gif驗證用戶密碼:
"^[a-zA-Z]/w{5,17}$"正確格式爲:以字母開頭,長度在6~18之間,只能包含字符、數字和下劃線。
None.gif驗證是否含有
^%&',;=?$/"等字符:"[^%&',;=?$/x22]+"
None.gif
只能輸入漢字:"^[/u4e00-/u9fa5]{0,}$"
None.gif驗證Email地址:
"^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$"
None.gif驗證InternetURL:
"^http://([/w-]+/.)+[/w-]+(/[/w-./?%&=]*)?$"
None.gif驗證電話號碼:
"^(/(/d{3,4}-)|/d{3.4}-)?/d{7,8}$"正確格式爲:"XXX-XXXXXXX""XXXX-XXXXXXXX""XXX-XXXXXXX""XXX-XXXXXXXX""XXXXXXX""XXXXXXXX"
None.gif驗證身份證號(15位或18位數字):
"^/d{15}|/d{18}$"
None.gif驗證一年的12個月:
"^(0?[1-9]|1[0-2])$"正確格式爲:"01""09""1""12"
None.gif驗證一個月的31天:
"^((0?[1-9])|((1|2)[0-9])|30|31)$"正確格式爲;"01""09""1""31"。 
None.gif
None.gif利用正則表達式限制網頁表單裏的文本框輸入內容:
None.gif
None.gif用正則表達式限制只能輸入中文:onkeyup
="value=value.replace(/[^/u4E00-/u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/u4E00-/u9FA5]/g,''))"
None.gif
None.gif用正則表達式限制只能輸入全角字符: onkeyup
="value=value.replace(/[^/uFF00-/uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/uFF00-/uFFFF]/g,''))"
None.gif
None.gif用正則表達式限制只能輸入數字:onkeyup
="value=value.replace(/[^/d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))"
None.gif
None.gif用正則表達式限制只能輸入數字和英文:onkeyup
="value=value.replace(/[/W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))"
None.gif
None.gif得用正則表達式從URL地址中提取文件名的javascript程序,如下結果爲page1
None.gif
None.gifs
="http://www.9499.net/page1.htm"
ExpandedBlockStart.gifs
=s.replace(/(.*//){0,}([^/.]+).*/ig,"$2")
None.gifalert(s)
None.gif
None.gif
None.gif匹配雙字節字符(包括漢字在內):[
^/x00-/xff]
None.gif
None.gif應用:計算字符串的長度(一個雙字節字符長度計2,ASCII字符計1)
None.gif
ExpandedBlockStart.gifString.prototype.len
=function(){return this.replace([^/x00-/xff]/g,"aa").length;}
None.gif
None.gif匹配空行的正則表達式:/n[/s
| ]*/r
None.gif
None.gif匹配HTML標記的正則表達式:
/<(.*)>.*<///1>|<(.*) //>/ 
None.gif
None.gif匹配首尾空格的正則表達式:(
^/s*)|(/s*$)
None.gif
None.gif
None.gifString.prototype.trim 
= function()
ExpandedBlockStart.gif
{
InBlock.gif
return this.replace(/(^/s*)|(/s*$)/g, "");
ExpandedBlockEnd.gif}

None.gif
None.gif利用正則表達式分解和轉換IP地址:
None.gif
None.gif下面是利用正則表達式匹配IP地址,並將IP地址轉換成對應數值的javascript程序:
None.gif
None.giffunction IP2V(ip)
ExpandedBlockStart.gif
{
InBlock.gifre
=/(/d+)/.(/d+)/.(/d+)/.(/d+)///匹配IP地址的正則表達式
InBlock.gif
if(re.test(ip))
ExpandedSubBlockStart.gif
{
InBlock.gif
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gif
{
InBlock.gif
throw new Error("Not a valid IP address!")
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif不過上面的程序如果不用正則表達式,而直接用split函數來分解可能更簡單,程序如下:
None.gif
None.gifvar ip
="10.100.20.168"
None.gifip
=ip.split(".")
None.gifalert(
"IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章