C#验证类 可验证:邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP
Posted on 2008-07-19 20:04 yongfa365 阅读(3501) 评论(6) 编辑 收藏 举报
///
/// RegExp Soruce: http://regexlib.com/DisplayPatterns.aspx
/// Author:柳永法 yongfa365 http://www.yongfa365.com/ yongfa365@qq.com
/// Intro:验证 网址,IP,邮箱,电话,手机,数字,英文,日期,身份证,邮编,
/// 原则上是中国通用,因为各种场合不一样所以有特殊情况肯定要自己再手写,这里只能是提供一些通用的验证,追求太完美是不现实的。
/// Version: 1.0
/// PutTime: 2008-6-5
/// LastModi:2008-6-5
///
1namespace 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 验证IP#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 是不是Int型的#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 是不是中国电话,格式010-85849685#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 邮政编码 6个数字#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 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 验证IP#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 是不是Int型的#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 是不是中国电话,格式010-85849685#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 邮政编码 6个数字#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}