人民医院API

ValidateString.cs 8.6KB

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