暂无描述

ValidateString.cs 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace CallCenter.Utility
  4. {
  5. public class ValidateString
  6. {
  7. #region 字符串验证
  8. #region 判断字符串是否为整型
  9. /**/
  10. /// <summary>
  11. /// 判断字符串是否为整型
  12. /// </summary>
  13. /// <param name="source"></param>
  14. /// <returns></returns>
  15. #endregion
  16. public static bool IsInteger(string source)
  17. {
  18. if (string.IsNullOrEmpty(source))
  19. {
  20. return false;
  21. }
  22. if (Regex.IsMatch(source, "^((\\+)\\d)?\\d*$"))
  23. {
  24. return true;
  25. }
  26. return false;
  27. }
  28. public static bool IsZInteger(string source)
  29. {
  30. if (string.IsNullOrEmpty(source))
  31. {
  32. return false;
  33. }
  34. if (Regex.IsMatch(source, "^[1-9][0-9]*$"))
  35. {
  36. return true;
  37. }
  38. return false;
  39. }
  40. #region 判断字符串是否为整型
  41. /**/
  42. /// <summary>
  43. /// 判断字符串是否为整型
  44. /// </summary>
  45. /// <param name="source"></param>
  46. /// <returns></returns>
  47. #endregion
  48. public static bool IsDecimal(string source)
  49. {
  50. return IsMoney(source);
  51. }
  52. #region Email 格式是否合法
  53. /**/
  54. /// <summary>
  55. /// Email 格式是否合法
  56. /// </summary>
  57. /// <param name="strEmail"></param>
  58. #endregion
  59. public static bool IsEmail(string strEmail)
  60. {
  61. return Regex.IsMatch(strEmail, @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
  62. }
  63. #region 判断一个字符串是否为网址
  64. /**/
  65. /// <summary>
  66. /// 判断一个字符串是否为网址
  67. /// </summary>
  68. /// <param name="_value"></param>
  69. /// <returns></returns>
  70. #endregion
  71. public static bool IsUrl(string _value)
  72. {
  73. var regex = new Regex(@"(http://)?([\w-]+\.)*[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.IgnoreCase);
  74. return regex.Match(_value).Success;
  75. }
  76. #region 判断是否IP
  77. /**/
  78. /// <summary>
  79. /// 判断是否IP
  80. /// </summary>
  81. /// <param name="source"></param>
  82. /// <returns></returns>
  83. #endregion
  84. public static bool IsIP(string source)
  85. {
  86. return Regex.IsMatch(source, @"^(((25[0-5]|2[0-4][0-9]|19[0-1]|19[3-9]|18[0-9]|17[0-1]|17[3-9]|1[0-6][0-9]|1[1-9]|[2-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))|(192\.(25[0-5]|2[0-4][0-9]|16[0-7]|169|1[0-5][0-9]|1[7-9][0-9]|[1-9][0-9]|[0-9]))|(172\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|1[0-5]|3[2-9]|[4-9][0-9]|[0-9])))\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$");
  87. }
  88. #region 检查字符串是否为A-Z、0-9及下划线以内的字符
  89. /**/
  90. /// <summary>
  91. /// 检查字符串是否为A-Z、0-9及下划线以内的字符
  92. /// </summary>
  93. /// <param name="str">被检查的字符串</param>
  94. /// <returns>是否有特殊字符</returns>
  95. #endregion
  96. public static bool IsLetterOrNumber(string str)
  97. {
  98. bool b = Regex.IsMatch(str, "[a-zA-Z0-9_]");
  99. return b;
  100. }
  101. #region 检查字符串是否0-9以内的字符
  102. /**/
  103. /// <summary>
  104. /// 检查字符串是否为0-9以内的字符
  105. /// </summary>
  106. /// <param name="str">被检查的字符串</param>
  107. /// <returns>是否有特殊字符</returns>
  108. #endregion
  109. public static bool IsNumberStr(string str)
  110. {
  111. bool b = Regex.IsMatch(str, "^[0-9]*$");
  112. return b;
  113. }
  114. #region 返回字符串某个位置的一个字符的类型
  115. /**/
  116. /// <summary>
  117. /// 返回字符串某个位置的几个字符是否为a-zA-Z的字母
  118. /// </summary>
  119. /// <param name="str">被检查的字符串</param>
  120. /// <param name="Site">字符位置</param>
  121. /// <param name="num">长度</param>
  122. /// <returns>是否是字母</returns>
  123. #endregion
  124. public static bool GetStringType(string str, int Site, int num)
  125. {
  126. string a = str.Substring(Site, num);
  127. bool b = Regex.IsMatch(a, "[a-zA-Z]");
  128. return b;
  129. }
  130. #region 验输入字符串是否含有“/\<>:.?*|$]”特殊字符
  131. /**/
  132. /// <summary>
  133. /// 验输入字符串是否含有“/\:.?*|$]”特殊字符
  134. /// </summary>
  135. /// <param name="source"></param>
  136. /// <returns></returns>
  137. #endregion
  138. public static bool IsSpecialChar(string source)
  139. {
  140. var r = new Regex(@"[/\<>:.?*|$]");
  141. return r.IsMatch(source);
  142. }
  143. #region 是否全为中文/日文/韩文字符
  144. /**/
  145. /// <summary>
  146. /// 是否全为中文/日文/韩文字符
  147. /// </summary>
  148. /// <param name="source">源字符串</param>
  149. /// <returns></returns>
  150. #endregion
  151. public static bool IsChineseChar(string source)
  152. {
  153. //中文/日文/韩文: [\u4E00-\u9FA5]
  154. //英文:[a-zA-Z]
  155. return Regex.IsMatch(source, @"^[\u4E00-\u9FA5]+$");
  156. }
  157. #region 是否包含双字节字符(允许有单字节字符)
  158. /**/
  159. /// <summary>
  160. /// 是否包含双字节字符(允许有单字节字符)
  161. /// </summary>
  162. /// <param name="source"></param>
  163. /// <returns></returns>
  164. #endregion
  165. public static bool IsDoubleChar(string source)
  166. {
  167. return Regex.IsMatch(source, @"[^\x00-\xff]");
  168. }
  169. #region 是否为日期型字符串
  170. /**/
  171. /// <summary>
  172. /// 是否为日期型字符串
  173. /// </summary>
  174. /// <param name="source">日期字符串(2005-6-30)</param>
  175. /// <returns></returns>
  176. #endregion
  177. public static bool IsDate(string source)
  178. {
  179. return Regex.IsMatch(source, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
  180. }
  181. #region 是否为时间型字符串
  182. /**/
  183. /// <summary>
  184. /// 是否为时间型字符串
  185. /// </summary>
  186. /// <param name="source">时间字符串(15:00:00)</param>
  187. /// <returns></returns>
  188. #endregion
  189. public static bool IsTime(string source)
  190. {
  191. return Regex.IsMatch(source, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
  192. }
  193. #region 是否为日期+时间型字符串
  194. /**/
  195. /// <summary>
  196. /// 是否为日期+时间型字符串
  197. /// </summary>
  198. /// <param name="source"></param>
  199. /// <returns></returns>
  200. #endregion
  201. public static bool IsDateTime(string source)
  202. {
  203. return Regex.IsMatch(source, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) ((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
  204. }
  205. #region 是否为钱币型数据
  206. /**/
  207. /// <summary>
  208. /// 是否为钱币型数据
  209. /// </summary>
  210. /// <param name="source"></param>
  211. /// <returns></returns>
  212. #endregion
  213. public static bool IsMoney(string source)
  214. {
  215. if (source == "")
  216. {
  217. return false;
  218. }
  219. return Regex.IsMatch(source, @"^(([1-9]\d*)|0)(\.\d{1,2})?$");
  220. }
  221. #region 判断是否为空字符串
  222. /**/
  223. /// <summary>
  224. /// 是否为空字符串
  225. /// </summary>
  226. /// <param name="obj"></param>
  227. /// <returns></returns>
  228. #endregion
  229. public static bool IsNull(object obj)
  230. {
  231. bool rn = obj == null || Convert.ToString(obj).Length <= 0 || Convert.IsDBNull(obj);
  232. return rn;
  233. }
  234. #endregion
  235. }
  236. }