足力健后端,使用.netcore版本,合并1个项目使用

ValidateHelper.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using Microsoft.AspNetCore.Mvc.ViewFeatures;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. namespace System.Common
  10. {
  11. /*
  12. * https://github.com/arasatasaygin/is.js/blob/master/is.js
  13. *
  14. var regexes = {
  15. affirmative: /^(?:1|t(?:rue)?|y(?:es)?|ok(?:ay)?)$/,
  16. alphaNumeric: /^[A-Za-z0-9]+$/,
  17. caPostalCode: /^(?!.*[DFIOQU])[A-VXY][0-9][A-Z]\s?[0-9][A-Z][0-9]$/,
  18. creditCard: /^(?:(4[0-9]{12}(?:[0-9]{3})?)|(5[1-5][0-9]{14})|(6(?:011|5[0-9]{2})[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])[0-9]{11})|((?:2131|1800|35[0-9]{3})[0-9]{11}))$/,
  19. dateString: /^(1[0-2]|0?[1-9])([\/-])(3[01]|[12][0-9]|0?[1-9])(?:\2)(?:[0-9]{2})?[0-9]{2}$/,
  20. email: /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i, // eslint-disable-line no-control-regex
  21. eppPhone: /^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/,
  22. hexadecimal: /^(?:0x)?[0-9a-fA-F]+$/,
  23. hexColor: /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/,
  24. ipv4: /^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/,
  25. ipv6: /^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i,
  26. nanpPhone: /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/,
  27. socialSecurityNumber: /^(?!000|666)[0-8][0-9]{2}-?(?!00)[0-9]{2}-?(?!0000)[0-9]{4}$/,
  28. timeString: /^(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$/,
  29. ukPostCode: /^[A-Z]{1,2}[0-9RCHNQ][0-9A-Z]?\s?[0-9][ABD-HJLNP-UW-Z]{2}$|^[A-Z]{2}-?[0-9]{4}$/,
  30. url: /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/i,
  31. usZipCode: /^[0-9]{5}(?:-[0-9]{4})?$/
  32. };
  33. */
  34. /// <summary>
  35. /// 验证帮助类
  36. /// </summary>
  37. public static class ValidateHelper
  38. {
  39. private static readonly Regex RegNumber = new Regex("^[0-9]+$", RegexOptions.Compiled);
  40. private static readonly Regex RegNumberSign = new Regex("^[+-]?[0-9]+$", RegexOptions.Compiled);
  41. private static readonly Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$", RegexOptions.Compiled);
  42. private static readonly Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$", RegexOptions.Compiled); //等价于^[+-]?\d+[.]?\d+$
  43. private static readonly Regex RegEmail = new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$", RegexOptions.Compiled);//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
  44. private static readonly Regex RegCHZN = new Regex("[\u4e00-\u9fa5]", RegexOptions.Compiled);
  45. /// <summary>
  46. /// 判断是否是邮件地址,来自nop的方法
  47. /// </summary>
  48. /// <param name="s"></param>
  49. /// <returns></returns>
  50. public static bool IsEmail(string s)
  51. {
  52. return IsPlumpString(s) && RegexHelper.IsMatch(s, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
  53. }
  54. /// <summary>
  55. /// 判断是不是url
  56. /// </summary>
  57. /// <param name="s"></param>
  58. /// <returns></returns>
  59. public static bool IsURL(string s)
  60. {
  61. return IsPlumpString(s) && RegexHelper.IsMatch(s, @"[a-zA-z]+://[^s]*");
  62. }
  63. /// <summary>
  64. /// 是手机号
  65. /// </summary>
  66. /// <param name="s"></param>
  67. /// <returns></returns>
  68. public static bool IsMobilePhone(string s)
  69. {
  70. return IsPlumpString(s) && RegexHelper.IsMatch(s, @"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");
  71. }
  72. /// <summary>
  73. /// 是否为固话号
  74. /// </summary>
  75. public static bool IsPhone(string s)
  76. {
  77. return IsPlumpString(s) && RegexHelper.IsMatch(s, @"^(\d{3,4}-?)?\d{7,8}$");
  78. }
  79. /// <summary>
  80. /// 是否是域名
  81. /// </summary>
  82. /// <param name="s"></param>
  83. /// <returns></returns>
  84. public static bool IsDomain(string s)
  85. {
  86. return IsPlumpString(s) && RegexHelper.IsMatch(s, @"^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$");
  87. }
  88. /// <summary>
  89. /// 是否是中文
  90. /// </summary>
  91. /// <param name="s"></param>
  92. /// <returns></returns>
  93. public static bool IsChinese(string s)
  94. {
  95. return IsPlumpString(s) && RegexHelper.IsMatch(s, @"^[\u4e00-\u9fa5]{0,}$");
  96. }
  97. /// <summary>
  98. /// 是否是身份证号
  99. /// </summary>
  100. /// <param name="s"></param>
  101. /// <returns></returns>
  102. public static bool IsIDCardNo(string s)
  103. {
  104. return IsPlumpString(s) && RegexHelper.IsMatch(s, @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
  105. }
  106. /// <summary>
  107. /// 是否为日期
  108. /// </summary>
  109. public static bool IsDate(string s)
  110. {
  111. //return IsPlumpString(s) && RegexHelper.IsMatch(s, @"(\d{4})-(\d{1,2})-(\d{1,2})");
  112. return DateTime.TryParse(s, out var re);
  113. }
  114. /// <summary>
  115. /// 是否是IP
  116. /// </summary>
  117. public static bool IsIP(string s) =>
  118. IsPlumpString(s) && RegexHelper.IsMatch(s, @"\d+\.\d+\.\d+\.\d+");
  119. public static bool IsIPv4(string s) =>
  120. IsPlumpString(s) && RegexHelper.IsMatch(s, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
  121. public static bool IsIPv6(string s) =>
  122. IsPlumpString(s) && RegexHelper.IsMatch(s, @"/^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i");
  123. /// <summary>
  124. /// 是否是时间
  125. /// </summary>
  126. /// <param name="s"></param>
  127. /// <returns></returns>
  128. public static bool IsTime(string s)
  129. {
  130. return IsPlumpString(s) && RegexHelper.IsMatch(s, @"^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$");
  131. }
  132. /// <summary>
  133. /// 数字或者字母
  134. /// </summary>
  135. /// <param name="s"></param>
  136. /// <returns></returns>
  137. public static bool IsNUMBER_OR_CHAR(string s)
  138. {
  139. return IsPlumpString(s) && RegexHelper.IsMatch(s, @"^[A-Za-z0-9]{4,40}$");
  140. }
  141. /// <summary>
  142. /// 是否是数字
  143. /// </summary>
  144. /// <param name="s"></param>
  145. /// <returns></returns>
  146. public static bool IsNumber(string s)
  147. {
  148. return IsPlumpString(s) && RegexHelper.IsMatch(s, @"^[0-9]*$");
  149. }
  150. /// <summary>
  151. /// 是否是数值(包括整数和小数)
  152. /// </summary>
  153. public static bool IsFloat(string s)
  154. {
  155. return float.TryParse(s, out var re);
  156. }
  157. /// <summary>
  158. /// 是否是double
  159. /// </summary>
  160. /// <param name="s"></param>
  161. /// <returns></returns>
  162. public static bool IsDouble(string s)
  163. {
  164. return double.TryParse(s, out var re);
  165. }
  166. /// <summary>
  167. /// 判断是数字,空返回false(前面可以带负号)
  168. /// </summary>
  169. /// <param name="s"></param>
  170. /// <returns></returns>
  171. public static bool IsInt(string s)
  172. {
  173. return int.TryParse(s, out var re);
  174. }
  175. /// <summary>
  176. /// 判断是字母,空返回false
  177. /// </summary>
  178. /// <param name="s"></param>
  179. /// <returns></returns>
  180. public static bool IsChar(string s)
  181. {
  182. if (!IsPlumpString(s)) { return false; }
  183. return s.ToArray().All(x => (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'));
  184. }
  185. /// <summary>
  186. /// 判断是否是中文字符串
  187. /// </summary>
  188. /// <param name="str"></param>
  189. /// <returns></returns>
  190. public static bool IsChineaseStr(string str)
  191. {
  192. if (!IsPlumpString(str))
  193. {
  194. return false;
  195. }
  196. return str.ToArray().All(x => x >= 0x4e00 && x <= 0x9fbb);
  197. }
  198. /// <summary>
  199. /// 简单判断是否是json,不一定全部正确
  200. /// </summary>
  201. /// <param name="json"></param>
  202. /// <returns></returns>
  203. public static bool IsJson(string json)
  204. {
  205. try
  206. {
  207. return JsonConvert.DeserializeObject(json) != null;
  208. }
  209. catch
  210. {
  211. return false;
  212. }
  213. }
  214. /// <summary>
  215. /// 检查颜色值是否为3/6位的合法颜色只支持#ffffff格式,rgb(0,0,0)格式不能验证通过
  216. /// </summary>
  217. /// <param name="color">待检查的颜色</param>
  218. /// <returns></returns>
  219. public static bool IsColor(string color)
  220. {
  221. if (!IsPlumpString(color)) { return false; }
  222. color = color.Trim();
  223. if (color.StartsWith("#")) { return false; }
  224. color = color.Trim('#');
  225. if (color.Length != 3 && color.Length != 6) { return false; }
  226. //不包含0-9 a-f以外的字符
  227. if (!RegexHelper.IsMatch(color, "[^0-9a-f]", RegexOptions.IgnoreCase))
  228. {
  229. return true;
  230. }
  231. return false;
  232. }
  233. public static readonly string[] ImageExtesions = new string[] { ".jpg", ".png", ".gif", ".bmp", ".jpeg" };
  234. /// <summary>
  235. /// 是否是图片
  236. /// </summary>
  237. /// <param name="urlOrPathOrName"></param>
  238. /// <returns></returns>
  239. public static bool IsImage(string urlOrPathOrName)
  240. {
  241. urlOrPathOrName = ConvertHelper.GetString(urlOrPathOrName).Trim().ToLower();
  242. foreach (var ext in ImageExtesions)
  243. {
  244. if (urlOrPathOrName.EndsWith(ext)) { return true; }
  245. }
  246. return false;
  247. }
  248. /// <summary>
  249. /// 判断文件流是否为UTF8字符集
  250. /// </summary>
  251. /// <param name="stream">文件流</param>
  252. /// <returns>判断结果</returns>
  253. private static bool IsUTF8(FileStream stream)
  254. {
  255. byte cOctets = 0; // octets to go in this UTF-8 encoded character
  256. byte chr;
  257. bool bAllAscii = true;
  258. for (int i = 0; i < stream.Length; ++i)
  259. {
  260. chr = (byte)stream.ReadByte();
  261. if ((chr & 0x80) != 0) { bAllAscii = false; }
  262. if (cOctets == 0)
  263. {
  264. if (chr >= 0x80)
  265. {
  266. do
  267. {
  268. chr <<= 1;
  269. cOctets++;
  270. }
  271. while ((chr & 0x80) != 0);
  272. cOctets--;
  273. if (cOctets == 0) { return false; }
  274. }
  275. }
  276. else
  277. {
  278. if ((chr & 0xC0) != 0x80) { return false; }
  279. cOctets--;
  280. }
  281. }
  282. if (cOctets > 0)
  283. {
  284. return false;
  285. }
  286. if (bAllAscii)
  287. {
  288. return false;
  289. }
  290. return true;
  291. }
  292. #region 判断是否是空数据
  293. /// <summary>
  294. /// 判断是否是有值的list
  295. /// </summary>
  296. public static bool IsPlumpList<T>(IEnumerable<T> list) => list?.Any() ?? false;
  297. /// <summary>
  298. /// 包含长度大于0的item,并把他们找出来
  299. /// </summary>
  300. public static bool IsPlumpListAfterFilterMeaninglessData(IEnumerable<string> list, out List<string> filtered)
  301. {
  302. filtered = ConvertHelper.NotNullList(list).Where(x => IsPlumpString(x)).ToList();
  303. return filtered.Any();
  304. }
  305. /// <summary>
  306. /// 判断是否是有值的字典
  307. /// </summary>
  308. public static bool IsPlumpDict<K, V>(IDictionary<K, V> dict) => dict?.Any() ?? false;
  309. /// <summary>
  310. /// 去除两端空格后判断是否是非空字符串
  311. /// </summary>
  312. public static bool IsPlumpStringAfterTrim(string str) => (str?.Trim().Length ?? 0) > 0;
  313. /// <summary>
  314. /// 判断是否是非空字符串
  315. /// </summary>
  316. public static bool IsPlumpString(string str) => (str?.Length ?? 0) > 0;
  317. /// <summary>
  318. /// 判断是否都是非空字符串
  319. /// </summary>
  320. public static bool IsAllPlumpString(params string[] strs)
  321. {
  322. if (!IsPlumpList(strs)) { throw new Exception("至少需要一个参数"); }
  323. return strs.All(x => IsPlumpString(x));
  324. }
  325. /// <summary>
  326. /// 判断数组里至少有一个非空字符串
  327. /// </summary>
  328. public static bool IsAnyPlumpString(params string[] strs)
  329. {
  330. if (!IsPlumpList(strs)) { throw new Exception("至少需要一个参数"); }
  331. return strs.Any(x => IsPlumpString(x));
  332. }
  333. #endregion
  334. /// <summary>
  335. /// 判断字符串的长度是否在范围之内,str可以为空
  336. /// </summary>
  337. public static bool IsLenInRange(string str, int min, int max)
  338. {
  339. var len = str?.Length ?? 0;
  340. return len >= min && len <= max;
  341. }
  342. /// <summary>
  343. /// 判断一个对象是否是某个类型
  344. /// </summary>
  345. public static bool Is<T>(object obj)
  346. {
  347. return obj != null && obj is T;
  348. }
  349. /// <summary>
  350. /// 判断是相同引用
  351. /// </summary>
  352. public static bool IsReferenceEquals(object obj1, object obj2)
  353. {
  354. return object.ReferenceEquals(obj1, obj2);
  355. }
  356. /// <summary>
  357. /// 两个json有相同的结构
  358. /// </summary>
  359. /// <param name="tuple"></param>
  360. /// <returns></returns>
  361. public static bool SameJsonStructure(this (string, string) tuple)
  362. {
  363. return JsonHelper.HasSameStructure(tuple.Item1, tuple.Item2);
  364. }
  365. }
  366. }