C#验证类(使用正则表达式)
1using System;
2using System.Text.RegularExpressions;
3
4namespace bobomousecom.crm
5{
6 /**//**//**//// <summary>
7 /// Regexlib 的摘要说明。
8 /// </summary>
9 public class Regexlib
10 {
11 public Regexlib()
12 {
13 //
14 // TODO: 在此处添加构造函数逻辑
15 //
16 }
17
18
19 //搜索输入字符串并返回所有 href=“”值
20 string DumpHrefs(String inputString)
21 {
22 Regex r;
23 Match m;
24
25 r = new Regex("href\s*=\s*(?:"(?<1>[^"]*)"|(?<1>\S+))",
26 RegexOptions.IgnoreCase|RegexOptions.Compiled);
27 for (m = r.Match(inputString); m.Success; m = m.NextMatch())
28 {
29 return("Found href " + m.Groups[1]);
30 }
31 }
32
33
34
35 //验证Email地址
36 bool IsValidEmail(string strIn)
37 {
38 // Return true if strIn is in valid e-mail format.
39 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})(]?)$");
40 }
41
42
43 //dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。
44 string MDYToDMY(String input)
45 {
46 return Regex.Replace(input,"\b(?\d{1,2})/(?\d{1,2})/(?\d{2,4})\b","${day}-${month}-${year}");
47 }
48
49
50 //验证是否为小数
51 bool IsValidDecimal(string strIn)
52 {
53
54 return Regex.IsMatch(strIn,@"[0].d{1,2}|[1]");
55 }
56
57
58 //验证是否为电话号码
59 bool IsValidTel(string strIn)
60 {
61 return Regex.IsMatch(strIn,@"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?");
62 }
63
64
65 //验证年月日
66 bool IsValidDate(string strIn)
67 {
68 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)$");
69 }
70
71
72 //验证后缀名
73 bool IsValidPostfix(string strIn)
74 {
75 return Regex.IsMatch(strIn,@".(?i:gif|jpg)$");
76 }
77
78
79 //验证字符是否在4至12之间
80 bool IsValidByte(string strIn)
81 {
82 return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");
83 }
84
85
86 //验证IP
87 bool IsValidIp(string strIn)
88 {
89 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])$");
90 }
91 }
92}
93
2using System.Text.RegularExpressions;
3
4namespace bobomousecom.crm
5{
6 /**//**//**//// <summary>
7 /// Regexlib 的摘要说明。
8 /// </summary>
9 public class Regexlib
10 {
11 public Regexlib()
12 {
13 //
14 // TODO: 在此处添加构造函数逻辑
15 //
16 }
17
18
19 //搜索输入字符串并返回所有 href=“”值
20 string DumpHrefs(String inputString)
21 {
22 Regex r;
23 Match m;
24
25 r = new Regex("href\s*=\s*(?:"(?<1>[^"]*)"|(?<1>\S+))",
26 RegexOptions.IgnoreCase|RegexOptions.Compiled);
27 for (m = r.Match(inputString); m.Success; m = m.NextMatch())
28 {
29 return("Found href " + m.Groups[1]);
30 }
31 }
32
33
34
35 //验证Email地址
36 bool IsValidEmail(string strIn)
37 {
38 // Return true if strIn is in valid e-mail format.
39 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})(]?)$");
40 }
41
42
43 //dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。
44 string MDYToDMY(String input)
45 {
46 return Regex.Replace(input,"\b(?\d{1,2})/(?\d{1,2})/(?\d{2,4})\b","${day}-${month}-${year}");
47 }
48
49
50 //验证是否为小数
51 bool IsValidDecimal(string strIn)
52 {
53
54 return Regex.IsMatch(strIn,@"[0].d{1,2}|[1]");
55 }
56
57
58 //验证是否为电话号码
59 bool IsValidTel(string strIn)
60 {
61 return Regex.IsMatch(strIn,@"(d+-)?(d{4}-?d{7}|d{3}-?d{8}|^d{7,8})(-d+)?");
62 }
63
64
65 //验证年月日
66 bool IsValidDate(string strIn)
67 {
68 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)$");
69 }
70
71
72 //验证后缀名
73 bool IsValidPostfix(string strIn)
74 {
75 return Regex.IsMatch(strIn,@".(?i:gif|jpg)$");
76 }
77
78
79 //验证字符是否在4至12之间
80 bool IsValidByte(string strIn)
81 {
82 return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");
83 }
84
85
86 //验证IP
87 bool IsValidIp(string strIn)
88 {
89 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])$");
90 }
91 }
92}
93