Validator.cs 验证类
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Text.RegularExpressions;
5
6
namespace ChinaValue.CommonV2008
7
{
8
/// <summary>
9
/// 各种数据检测的逻辑
10
/// </summary>
11
public class Validator
12
{
13
/// <summary>
14
/// 检测手机号码是否有效
15
/// </summary>
16
/// <param name="strMobile"></param>
17
/// <returns></returns>
18
public static Boolean IsMobile(String strMobile)
19
{
20
//检测逻辑是是否11位的数字
21
if (IsNumeric(strMobile))
22
{
23
if (strMobile.Length == 11)
24
return true;
25
else
26
return false;
27
}
28
else
29
return false;
30
}
31
32
/// <summary>
33
/// 检测字符串的内容是否数字
34
/// </summary>
35
/// <param name="strTemp"></param>
36
/// <returns></returns>
37
public static Boolean IsNumeric(String strTemp)
38
{
39
//为了检测类似1,235,000的数据,需要将半角逗号过滤
40
strTemp = strTemp.Replace(",", String.Empty);
41
42
Regex regNum = new Regex(@"^[-]?\d+[.]?\d*$");
43
return regNum.IsMatch(strTemp);
44
}
45
46
/// <summary>
47
/// 检测字符串的内容是否是email
48
/// </summary>
49
/// <param name="email"></param>
50
/// <returns></returns>
51
public static Boolean IsEmail(String email)
52
{
53
string strRegTxt = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
54
if (!Regex.IsMatch(email, strRegTxt))
55
{
56
return false;
57
}
58
else
59
{
60
return true;
61
}
62
}
63
64
/// <summary>
65
/// 检测字符串是否URL
66
/// </summary>
67
/// <param name="url"></param>
68
/// <returns></returns>
69
public static Boolean IsURL(String url)
70
{
71
//去掉可能引起误会的字符
72
url = url.Replace("@", String.Empty);
73
74
string strRegTxt = @"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$";
75
76
if (Regex.IsMatch(url, strRegTxt))
77
{
78
return true;
79
}
80
81
return false;
82
}
83
84
/// <summary>
85
/// 是否中文字符
86
/// </summary>
87
/// <param name="str"></param>
88
/// <returns></returns>
89
public static Boolean IsCNChar(String str)
90
{
91
string strRegTxt = @"^[\u4e00-\u9fa5]{0,}$";
92
if (!Regex.IsMatch(str, strRegTxt))
93
{
94
return false;
95
}
96
else
97
{
98
return true;
99
}
100
}
101
}
102
}
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Text.RegularExpressions;5

6
namespace ChinaValue.CommonV20087
{8
/// <summary>9
/// 各种数据检测的逻辑10
/// </summary>11
public class Validator12
{13
/// <summary>14
/// 检测手机号码是否有效15
/// </summary>16
/// <param name="strMobile"></param>17
/// <returns></returns>18
public static Boolean IsMobile(String strMobile)19
{20
//检测逻辑是是否11位的数字21
if (IsNumeric(strMobile))22
{23
if (strMobile.Length == 11)24
return true;25
else26
return false;27
}28
else29
return false;30
}31

32
/// <summary>33
/// 检测字符串的内容是否数字34
/// </summary>35
/// <param name="strTemp"></param>36
/// <returns></returns>37
public static Boolean IsNumeric(String strTemp)38
{39
//为了检测类似1,235,000的数据,需要将半角逗号过滤40
strTemp = strTemp.Replace(",", String.Empty);41

42
Regex regNum = new Regex(@"^[-]?\d+[.]?\d*$");43
return regNum.IsMatch(strTemp);44
}45

46
/// <summary>47
/// 检测字符串的内容是否是email48
/// </summary>49
/// <param name="email"></param>50
/// <returns></returns>51
public static Boolean IsEmail(String email)52
{53
string strRegTxt = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";54
if (!Regex.IsMatch(email, strRegTxt))55
{56
return false;57
}58
else59
{60
return true;61
}62
}63

64
/// <summary>65
/// 检测字符串是否URL66
/// </summary>67
/// <param name="url"></param>68
/// <returns></returns>69
public static Boolean IsURL(String url)70
{71
//去掉可能引起误会的字符72
url = url.Replace("@", String.Empty);73

74
string strRegTxt = @"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$";75

76
if (Regex.IsMatch(url, strRegTxt))77
{78
return true;79
}80

81
return false;82
}83

84
/// <summary>85
/// 是否中文字符86
/// </summary>87
/// <param name="str"></param>88
/// <returns></returns>89
public static Boolean IsCNChar(String str)90
{91
string strRegTxt = @"^[\u4e00-\u9fa5]{0,}$";92
if (!Regex.IsMatch(str, strRegTxt))93
{94
return false;95
}96
else97
{98
return true;99
}100
}101
}102
}
Ajax.jQuery.Java.



浙公网安备 33010602011771号