市长热线演示版

RegexUtil.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * 版权所有:Copyright (C) 2013,河南华谊科技
  3.   * Version: 1.0
  4.   * 创建日期: 2013-05-16
  5.   * 作者: 魏飞
  6.   * 说明: 常用正则验证
  7.   * 修改日期: yyyy-MM-dd
  8.   * 修改者: 修改者
  9.   * 修改说明: 修改说明
  10. */
  11. using System;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. namespace HySoft.Common
  15. {
  16. /// <summary>
  17. /// 提供常用正则验证
  18. /// </summary>
  19. public class RegexUtil
  20. {
  21. /// <summary>
  22. /// 是否为纯数字
  23. /// </summary>
  24. /// <param name="str"></param>
  25. /// <returns></returns>
  26. public static bool IsNumber(string str)
  27. {
  28. return Regex.IsMatch(str, @"^\d+$");
  29. }
  30. /// <summary>
  31. /// 是否为纯字母
  32. /// </summary>
  33. /// <param name="str"></param>
  34. /// <returns></returns>
  35. public static bool IsLetter(string str)
  36. {
  37. return Regex.IsMatch(str, @"^[a-zA-Z]+$");
  38. }
  39. /// <summary>
  40. /// 是否为图片格式
  41. /// </summary>
  42. /// <param name="filename"></param>
  43. /// <returns></returns>
  44. public static bool IsImage(string filename)
  45. {
  46. return Regex.IsMatch(filename, @"(?i)\.(jpg|gif|png|bmp|tiff)$");
  47. }
  48. /// <summary>
  49. /// 验证是否为用户名格式(字母、数字、下划线、汉字)
  50. /// </summary>
  51. /// <param name="str"></param>
  52. /// <returns></returns>
  53. public static bool IsUsername(string str)
  54. {
  55. return Regex.IsMatch(str, @"^\w+$");
  56. }
  57. public static bool IsUrl(string str)
  58. {
  59. return Regex.IsMatch(str, @"(?i)^http(?s)://([\w\-]+\.)+[\w\-]+(/[\w\-./?%&=]*)?$");
  60. }
  61. public static bool IsEmail(string str)
  62. {
  63. return Regex.IsMatch(str, @"(?i)^([a-z0-9][a-z0-9_\-\.]+)@([a-z0-9][a-z0-9\.\-]{0,20})\.([a-z]{2,4})$");
  64. }
  65. public static bool IsIpAddress(string str)
  66. {
  67. return Regex.IsMatch(str, @"^(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])$");
  68. }
  69. /// <summary>
  70. /// 验证是否为身份证号
  71. /// </summary>
  72. /// <param name="str"></param>
  73. /// <returns></returns>
  74. public static bool IsIDCardNumber(string str)
  75. {
  76. if (str.Length == 15)
  77. {
  78. return Regex.IsMatch(str, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
  79. }
  80. else if (str.Length == 18)
  81. {
  82. return Regex.IsMatch(str, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$");
  83. }
  84. return false;
  85. }
  86. /// <summary>
  87. /// 是否为手机号
  88. /// </summary>
  89. /// <param name="str"></param>
  90. /// <returns></returns>
  91. public static bool IsMobile(string str)
  92. {
  93. return Regex.IsMatch(str, @"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$");
  94. }
  95. /// <summary>
  96. /// 是否为电话号
  97. /// </summary>
  98. /// <param name="str"></param>
  99. /// <returns></returns>
  100. public static bool IsTelphone(string str)
  101. {
  102. string patt = @"^((\d{7,8})|(\d{3,4})(-|\s)?(\d{7,8})|(\d{3,4})(-|\s)?(\d{7,8})(-|\s)(\d{1,4})|(\d{7,8})(-|\s)(\d{1,4}))$";
  103. return Regex.IsMatch(str, patt);
  104. }
  105. /// <summary>
  106. /// 是否为日期格式(不含时间)
  107. /// </summary>
  108. /// <param name="str"></param>
  109. /// <returns></returns>
  110. public static bool IsDate(string str)
  111. {
  112. string patt = @"^(?:(?!0000)[0-9]{4}([-/.\s]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.\s]?)0?2\2(?:29))$";
  113. return Regex.IsMatch(str, patt);
  114. }
  115. /// <summary>
  116. /// 是否为时间格式
  117. /// </summary>
  118. /// <param name="str"></param>
  119. /// <returns></returns>
  120. public static bool IsTime(string str)
  121. {
  122. return Regex.IsMatch(str, @"^([01]?[0-9]|2?[0-3]):[0-5]?[0-9](:[0-5]?[0-9])?$");
  123. }
  124. /// <summary>
  125. /// 是否为日期时间格式
  126. /// </summary>
  127. /// <param name="str"></param>
  128. /// <returns></returns>
  129. public static bool IsDateTime(string str)
  130. {
  131. string patt = @"^(?:(?!0000)[0-9]{4}([-/.\s]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.\s]?)0?2\2(?:29))\s+([01]?[0-9]|2?[0-3]):[0-5]?[0-9](:[0-5]?[0-9])?$";
  132. return Regex.IsMatch(str, patt);
  133. }
  134. /// <summary>
  135. /// 验证是否为用户名格式(字母、数字、下划线)
  136. /// </summary>
  137. /// <param name="str"></param>
  138. /// <returns></returns>
  139. public static bool IsUserName(string str)
  140. {
  141. return Regex.IsMatch(str, @"^[0-9a-zA-Z_]+$");
  142. }
  143. /// <summary>
  144. /// 验证是否为字母数字格式(字母、数字)
  145. /// </summary>
  146. /// <param name="str"></param>
  147. /// <returns></returns>
  148. public static bool IsOnlyLetterNumber(string str)
  149. {
  150. return Regex.IsMatch(str, @"^[0-9a-zA-Z]+$");
  151. }
  152. /// <summary>
  153. /// 是否为手机号或电话
  154. /// </summary>
  155. /// <param name="str"></param>
  156. /// <returns></returns>
  157. public static bool IsMobileOrTel(string str)
  158. {
  159. return Regex.IsMatch(str, @"^((\d{3,4}-\d{7,8})|(\d{11}))$");
  160. }
  161. /// <summary>
  162. /// 是否为11位数字
  163. /// </summary>
  164. /// <param name="str"></param>
  165. /// <returns></returns>
  166. public static bool IsMobile1(string str)
  167. {
  168. return Regex.IsMatch(str, @"^(\d{11})$");
  169. }
  170. /// <summary>
  171. /// 是否为ip
  172. /// </summary>
  173. /// <param name="ip"></param>
  174. /// <returns></returns>
  175. public static bool IsIP(string ip)
  176. {
  177. return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
  178. }
  179. }
  180. }