c#常用的验证类
1
using System;
2
using System.Text.RegularExpressions;
3
4
namespace CommonTools
5
{
6
/**//// <summary>
7
/// RegexLib 的摘要说明。
8
/// </summary>
9
public class RegexLib
10
{
11
12
//验证Email地址
13
public static bool IsValidEmail(string strIn)
14
{
15
// Return true if strIn is in valid e-mail format.
16
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]
17![]()
18
{2,4}|[0-9]{1,3})(\]?)$");
19
}
20
//dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。
21
public static string MDYToDMY(String input)
22
{
23
return Regex.Replace(input,"\\b(?\\d{1,2})/(?\\d{1,2})/(?\\d{2,4})\\b","${day}-${month}-${year}");
24
}
25
//验证是否为小数
26
public static bool IsValidDecimal(string strIn)
27
{
28
return Regex.IsMatch(strIn,@"[0].\d{1,2}|[1]");
29
}
30
//验证是否为电话号码
31
public static bool IsValidTel(string strIn)
32
{
33
return Regex.IsMatch(strIn,@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?");
34
}
35
//验证年月日
36
public static bool IsValidDate(string strIn)
37
{
38
return Regex.IsMatch(strIn,@"^2\d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]\d|3[0-1])(?:0?[1-9]|1\d|2[0-3]):
39![]()
40
(?:0?[1-9]|[1-5]\d):(?:0?[1-9]|[1-5]\d)$");
41
}
42
//验证后缀名
43
public static bool IsValidPostfix(string strIn)
44
{
45
return Regex.IsMatch(strIn,@"\.(?i:gif|jpg)$");
46
}
47
//验证字符是否在4至12之间
48
public static bool IsValidByte(string strIn)
49
{
50
return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");
51
}
52
//验证IP
53
public static bool IsValidIp(string strIn)
54
{
55
return Regex.IsMatch(strIn,@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1
56![]()
57
\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
58
}
59
60
}
61
}
62![]()
using System; 2
using System.Text.RegularExpressions; 3
4
namespace CommonTools 5
{ 6
/**//// <summary> 7
/// RegexLib 的摘要说明。 8
/// </summary> 9
public class RegexLib 10
{ 11
12
//验证Email地址 13
public static bool IsValidEmail(string strIn) 14
{ 15
// Return true if strIn is in valid e-mail format. 16
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]17

18
{2,4}|[0-9]{1,3})(\]?)$"); 19
} 20
//dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。 21
public static string MDYToDMY(String input) 22
{ 23
return Regex.Replace(input,"\\b(?\\d{1,2})/(?\\d{1,2})/(?\\d{2,4})\\b","${day}-${month}-${year}"); 24
} 25
//验证是否为小数 26
public static bool IsValidDecimal(string strIn) 27
{ 28
return Regex.IsMatch(strIn,@"[0].\d{1,2}|[1]"); 29
} 30
//验证是否为电话号码 31
public static bool IsValidTel(string strIn) 32
{ 33
return Regex.IsMatch(strIn,@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?"); 34
} 35
//验证年月日 36
public static bool IsValidDate(string strIn) 37
{ 38
return Regex.IsMatch(strIn,@"^2\d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]\d|3[0-1])(?:0?[1-9]|1\d|2[0-3]):39

40
(?:0?[1-9]|[1-5]\d):(?:0?[1-9]|[1-5]\d)$"); 41
} 42
//验证后缀名 43
public static bool IsValidPostfix(string strIn) 44
{ 45
return Regex.IsMatch(strIn,@"\.(?i:gif|jpg)$"); 46
} 47
//验证字符是否在4至12之间 48
public static bool IsValidByte(string strIn) 49
{ 50
return Regex.IsMatch(strIn,@"^[a-z]{4,12}$"); 51
} 52
//验证IP 53
public static bool IsValidIp(string strIn) 54
{ 55
return Regex.IsMatch(strIn,@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|156

57
\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"); 58
} 59
60
} 61
} 62




浙公网安备 33010602011771号