You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.7 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. namespace SqlSugar
  6. {
  7. internal static class ValidateExtensions
  8. {
  9. public static bool IsInRange(this int thisValue, int begin, int end)
  10. {
  11. return thisValue >= begin && thisValue <= end;
  12. }
  13. public static bool IsInRange(this DateTime thisValue, DateTime begin, DateTime end)
  14. {
  15. return thisValue >= begin && thisValue <= end;
  16. }
  17. public static bool IsIn<T>(this T thisValue, params T[] values)
  18. {
  19. return values.Contains(thisValue);
  20. }
  21. public static bool IsContainsIn(this string thisValue, params string[] inValues)
  22. {
  23. return inValues.Any(it => thisValue.Contains(it));
  24. }
  25. public static bool IsNullOrEmpty(this object thisValue)
  26. {
  27. if (thisValue == null || thisValue == DBNull.Value) return true;
  28. return thisValue.ToString() == "";
  29. }
  30. public static bool IsNullOrEmpty(this Guid? thisValue)
  31. {
  32. if (thisValue == null) return true;
  33. return thisValue == Guid.Empty;
  34. }
  35. public static bool IsNullOrEmpty(this Guid thisValue)
  36. {
  37. if (thisValue == null) return true;
  38. return thisValue == Guid.Empty;
  39. }
  40. public static bool IsNullOrEmpty(this IEnumerable<object> thisValue)
  41. {
  42. if (thisValue == null || thisValue.Count() == 0) return true;
  43. return false;
  44. }
  45. public static bool HasValue(this object thisValue)
  46. {
  47. if (thisValue == null || thisValue == DBNull.Value) return false;
  48. return thisValue.ToString() != "";
  49. }
  50. public static bool HasValue(this IEnumerable<object> thisValue)
  51. {
  52. if (thisValue == null || thisValue.Count() == 0) return false;
  53. return true;
  54. }
  55. public static bool IsValuable(this IEnumerable<KeyValuePair<string,string>> thisValue)
  56. {
  57. if (thisValue == null || thisValue.Count() == 0) return false;
  58. return true;
  59. }
  60. public static bool IsZero(this object thisValue)
  61. {
  62. return (thisValue == null || thisValue.ToString() == "0");
  63. }
  64. public static bool IsInt(this object thisValue)
  65. {
  66. if (thisValue == null) return false;
  67. return Regex.IsMatch(thisValue.ToString(), @"^\d+$");
  68. }
  69. /// <returns></returns>
  70. public static bool IsNoInt(this object thisValue)
  71. {
  72. if (thisValue == null) return true;
  73. return !Regex.IsMatch(thisValue.ToString(), @"^\d+$");
  74. }
  75. public static bool IsMoney(this object thisValue)
  76. {
  77. if (thisValue == null) return false;
  78. double outValue = 0;
  79. return double.TryParse(thisValue.ToString(), out outValue);
  80. }
  81. public static bool IsGuid(this object thisValue)
  82. {
  83. if (thisValue == null) return false;
  84. var outValue = Guid.Empty;
  85. return Guid.TryParse(thisValue.ToString(), out outValue);
  86. }
  87. public static bool IsDate(this object thisValue)
  88. {
  89. if (thisValue == null) return false;
  90. var outValue = DateTime.MinValue;
  91. return DateTime.TryParse(thisValue.ToString(), out outValue);
  92. }
  93. public static bool IsEamil(this object thisValue)
  94. {
  95. if (thisValue == null) return false;
  96. return Regex.IsMatch(thisValue.ToString(), @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");
  97. }
  98. public static bool IsMobile(this object thisValue)
  99. {
  100. if (thisValue == null) return false;
  101. return Regex.IsMatch(thisValue.ToString(), @"^\d{11}$");
  102. }
  103. public static bool IsTelephone(this object thisValue)
  104. {
  105. if (thisValue == null) return false;
  106. return System.Text.RegularExpressions.Regex.IsMatch(thisValue.ToString(), @"^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}$");
  107. }
  108. public static bool IsIDcard(this object thisValue)
  109. {
  110. if (thisValue == null) return false;
  111. return System.Text.RegularExpressions.Regex.IsMatch(thisValue.ToString(), @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
  112. }
  113. public static bool IsFax(this object thisValue)
  114. {
  115. if (thisValue == null) return false;
  116. return System.Text.RegularExpressions.Regex.IsMatch(thisValue.ToString(), @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
  117. }
  118. public static bool IsMatch(this object thisValue, string pattern)
  119. {
  120. if (thisValue == null) return false;
  121. var reg = new Regex(pattern);
  122. return reg.IsMatch(thisValue.ToString());
  123. }
  124. public static bool IsAnonymousType(this Type type)
  125. {
  126. var typeName = type.Name;
  127. return typeName.Contains("<>") && typeName.Contains("__") && typeName.Contains("AnonymousType");
  128. }
  129. public static bool IsCollectionsList(this string thisValue)
  130. {
  131. return (thisValue + "").StartsWith("System.Collections.Generic.List");
  132. }
  133. public static bool IsStringArray(this string thisValue)
  134. {
  135. return (thisValue + "").IsMatch(@"System\.[a-z,A-Z,0-9]+?\[\]");
  136. }
  137. public static bool IsEnumerable(this string thisValue)
  138. {
  139. return (thisValue + "").StartsWith("System.Linq.Enumerable");
  140. }
  141. public static Type StringType = typeof (string);
  142. public static bool IsClass(this Type thisValue)
  143. {
  144. return thisValue != StringType && thisValue.IsEntity();
  145. }
  146. }
  147. }