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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

Ajax.jQuery.Java.