C#驗證類 可驗證:郵箱,電話,手機,數字,英文,日期,身份證,郵編,網址,IP

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\&amp;%_\./-~-]*)?$", 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\&amp;%_\./-~-]*)?", 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(1016|| 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(68).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, 11out y);
155            if (arrVarifyCode[y] != Id.Substring(171).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(1014))
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(66).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}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章