1
namespace
YongFa365.Validator
2
{
3
using System;
4
using System.Text.RegularExpressions;
5
6
/// <summary>
7
/// RegExp Soruce: http://regexlib.com/DisplayPatterns.aspx
8
/// Author:柳永法 yongfa365 http://www.yongfa365.com/ yongfa365@qq.com
9
/// Intro:验证 网址,IP,邮箱,电话,手机,数字,英文,日期,身份证,邮编,
10
/// 原则上是中国通用,因为各种场合不一样所以有特殊情况肯定要自己再手写,这里只能是提供一些通用的验证,追求太完美是不现实的。
11
/// Version: 1.0
12
/// PutTime: 2008-6-5
13
/// LastModi:2008-6-5
14
/// </summary>
15
///
16
public class Validator
17
{
18
19
#region 验证邮箱
20
/// <summary>
21
/// 验证邮箱
22
/// </summary>
23
/// <param name="source"></param>
24
/// <returns></returns>
25
public static bool IsEmail(string source)
26
{
27
return Regex.IsMatch(source, @"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", RegexOptions.IgnoreCase);
28
}
29
public static bool HasEmail(string source)
30
{
31
return Regex.IsMatch(source, @"[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})", RegexOptions.IgnoreCase);
32
}
33
#endregion
34
35
#region 验证网址
36
/// <summary>
37
/// 验证网址
38
/// </summary>
39
/// <param name="source"></param>
40
/// <returns></returns>
41
public static bool IsUrl(string source)
42
{
43
return Regex.IsMatch(source, @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?$", RegexOptions.IgnoreCase);
44
}
45
public static bool HasUrl(string source)
46
{
47
return Regex.IsMatch(source, @"(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?", RegexOptions.IgnoreCase);
48
}
49
#endregion
50
51
#region 验证日期
52
/// <summary>
53
/// 验证日期
54
/// </summary>
55
/// <param name="source"></param>
56
/// <returns></returns>
57
public static bool IsDateTime(string source)
58
{
59
try
60
{
61
DateTime time = Convert.ToDateTime(source);
62
return true;
63
}
64
catch
65
{
66
return false;
67
}
68
}
69
#endregion
70
71
#region 验证手机号
72
/// <summary>
73
/// 验证手机号
74
/// </summary>
75
/// <param name="source"></param>
76
/// <returns></returns>
77
public static bool IsMobile(string source)
78
{
79
return Regex.IsMatch(source, @"^1[35]\d{9}$", RegexOptions.IgnoreCase);
80
}
81
public static bool HasMobile(string source)
82
{
83
return Regex.IsMatch(source, @"1[35]\d{9}", RegexOptions.IgnoreCase);
84
}
85
#endregion
86
87
#region 验证IP
88
/// <summary>
89
/// 验证IP
90
/// </summary>
91
/// <param name="source"></param>
92
/// <returns></returns>
93
public static bool IsIP(string source)
94
{
95
return Regex.IsMatch(source, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", RegexOptions.IgnoreCase);
96
}
97
public static bool HasIP(string source)
98
{
99
return Regex.IsMatch(source, @"(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])", RegexOptions.IgnoreCase);
100
}
101
#endregion
102
103
#region 验证身份证是否有效
104
/// <summary>
105
/// 验证身份证是否有效
106
/// </summary>
107
/// <param name="Id"></param>
108
/// <returns></returns>
109
public static bool IsIDCard(string Id)
110
{
111
if (Id.Length == 18)
112
{
113
bool check = IsIDCard18(Id);
114
return check;
115
}
116
else if (Id.Length == 15)
117
{
118
bool check = IsIDCard15(Id);
119
return check;
120
}
121
else
122
{
123
return false;
124
}
125
}
126
127
public static bool IsIDCard18(string Id)
128
{
129
long n = 0;
130
if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
131
{
132
return false;//数字验证
133
}
134
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
135
if (address.IndexOf(Id.Remove(2)) == -1)
136
{
137
return false;//省份验证
138
}
139
string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
140
DateTime time = new DateTime();
141
if (DateTime.TryParse(birth, out time) == false)
142
{
143
return false;//生日验证
144
}
145
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
146
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
147
char[] Ai = Id.Remove(17).ToCharArray();
148
int sum = 0;
149
for (int i = 0; i < 17; i++)
150
{
151
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
152
}
153
int y = -1;
154
Math.DivRem(sum, 11, out y);
155
if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
156
{
157
return false;//校验码验证
158
}
159
return true;//符合GB11643-1999标准
160
}
161
162
public static bool IsIDCard15(string Id)
163
{
164
long n = 0;
165
if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
166
{
167
return false;//数字验证
168
}
169
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
170
if (address.IndexOf(Id.Remove(2)) == -1)
171
{
172
return false;//省份验证
173
}
174
string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
175
DateTime time = new DateTime();
176
if (DateTime.TryParse(birth, out time) == false)
177
{
178
return false;//生日验证
179
}
180
return true;//符合15位身份证标准
181
}
182
#endregion
183
184
#region 是不是Int型的
185
/// <summary>
186
/// 是不是Int型的
187
/// </summary>
188
/// <param name="source"></param>
189
/// <returns></returns>
190
public static bool IsInt(string source)
191
{
192
Regex regex = new Regex(@"^(-){0,1}\d+$");
193
if (regex.Match(source).Success)
194
{
195
if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
196
{
197
return false;
198
}
199
return true;
200
}
201
return false;
202
}
203
#endregion
204
205
#region 看字符串的长度是不是在限定数之间 一个中文为两个字符
206
/// <summary>
207
/// 看字符串的长度是不是在限定数之间 一个中文为两个字符
208
/// </summary>
209
/// <param name="source">字符串</param>
210
/// <param name="begin">大于等于</param>
211
/// <param name="end">小于等于</param>
212
/// <returns></returns>
213
public static bool IsLengthStr(string source, int begin, int end)
214
{
215
int length = Regex.Replace(source, @"[^\x00-\xff]", "OK").Length;
216
if ((length <= begin) && (length >= end))
217
{
218
return false;
219
}
220
return true;
221
}
222
#endregion
223
224
#region 是不是中国电话,格式010-85849685
225
/// <summary>
226
/// 是不是中国电话,格式010-85849685
227
/// </summary>
228
/// <param name="source"></param>
229
/// <returns></returns>
230
public static bool IsTel(string source)
231
{
232
return Regex.IsMatch(source, @"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase);
233
}
234
#endregion
235
236
#region 邮政编码 6个数字
237
/// <summary>
238
/// 邮政编码 6个数字
239
/// </summary>
240
/// <param name="source"></param>
241
/// <returns></returns>
242
public static bool IsPostCode(string source)
243
{
244
return Regex.IsMatch(source, @"^\d{6}$", RegexOptions.IgnoreCase);
245
}
246
#endregion
247
248
#region 中文
249
/// <summary>
250
/// 中文
251
/// </summary>
252
/// <param name="source"></param>
253
/// <returns></returns>
254
public static bool IsChinese(string source)
255
{
256
return Regex.IsMatch(source, @"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
257
}
258
public static bool hasChinese(string source)
259
{
260
return Regex.IsMatch(source, @"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
261
}
262
#endregion
263
264
#region 验证是不是正常字符 字母,数字,下划线的组合
265
/// <summary>
266
/// 验证是不是正常字符 字母,数字,下划线的组合
267
/// </summary>
268
/// <param name="source"></param>
269
/// <returns></returns>
270
public static bool IsNormalChar(string source)
271
{
272
return Regex.IsMatch(source, @"[\w\d_]+", RegexOptions.IgnoreCase);
273
}
274
#endregion
275
276
}
277
}

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277
