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/ [email protected]
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
102
103 驗證身份證是否有效
183
184 是不是Int型的
204
205 看字符串的長度是不是在限定數之間 一箇中文爲兩個字符
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