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

posted on 2010-12-09 16:48  kitea  阅读(201)  评论(0)    收藏  举报