TValidationRule.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows.Controls;
  9. namespace FWindSoft.Wpf
  10. {
  11. /*
  12. DataErrorValidationRule,ExceptionValidationRule
  13. */
  14. public class TValidationRule: ValidationRule
  15. {
  16. /// <summary>
  17. /// 错误信息
  18. /// </summary>
  19. public string ErrorMessage { get; set; }
  20. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  21. {
  22. if (value == null)
  23. return new ValidationResult(false, ErrorMessage??"不能为空值!");
  24. if (string.IsNullOrWhiteSpace(value.ToString()))
  25. return new ValidationResult(false, ErrorMessage ?? "不能为空字符串!");
  26. return new ValidationResult(true, null);
  27. }
  28. }
  29. public class RequireValidationRule : TValidationRule
  30. {
  31. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  32. {
  33. if (value == null)
  34. return new ValidationResult(false, ErrorMessage ?? "不能为空值!");
  35. if (string.IsNullOrWhiteSpace(value.ToString()))
  36. return new ValidationResult(false, ErrorMessage ?? "不能为空字符串!");
  37. return new ValidationResult(true, null);
  38. }
  39. }
  40. #region 正则表达式判定
  41. /// <summary>
  42. /// 这则表达式验证基类
  43. /// </summary>
  44. public class RegexValidationRule : TValidationRule
  45. {
  46. protected string m_Match;
  47. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  48. {
  49. if (value==null||string.IsNullOrWhiteSpace(value.ToString()))
  50. return new ValidationResult(true, null);
  51. if (m_Match == null)
  52. return new ValidationResult(true, null);
  53. if (!string.IsNullOrWhiteSpace(value.ToString()))
  54. {
  55. Regex tempMatch = new Regex(m_Match);
  56. if (!tempMatch.IsMatch(value.ToString()))
  57. {
  58. return new ValidationResult(false, ErrorMessage ?? "信息错误");
  59. }
  60. }
  61. return new ValidationResult(true, null);
  62. }
  63. }
  64. public class EMailValidationRule : RegexValidationRule
  65. {
  66. public EMailValidationRule()
  67. {
  68. m_Match = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
  69. }
  70. }
  71. public class FaxValidationRule : RegexValidationRule
  72. {
  73. public FaxValidationRule()
  74. {
  75. m_Match = @"^(\d{3,4}-?)?\d{7,8}$";
  76. }
  77. }
  78. public class PhoneValidationRule : RegexValidationRule
  79. {
  80. public PhoneValidationRule()
  81. {
  82. m_Match = @"^(\d{3,4}-?)?\d{7,8}$";
  83. }
  84. }
  85. public class CustomRegexValidationRule : RegexValidationRule
  86. {
  87. public CustomRegexValidationRule()
  88. {
  89. }
  90. public string Match
  91. {
  92. get { return this.m_Match; }
  93. set { this.Match = value; }
  94. }
  95. }
  96. #endregion
  97. #region 使用委托判定
  98. public class MethodValidationRule : TValidationRule
  99. {
  100. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  101. {
  102. //if (value == null)
  103. // return new ValidationResult(false, ErrorMessage ?? "不能为空值!");
  104. if(Method!=null)
  105. return Method?.Invoke(value);
  106. return new ValidationResult(true, null);
  107. }
  108. public Func<object, ValidationResult> Method { get; set; }
  109. }
  110. #endregion
  111. #region 特殊判定
  112. public class ValidateInteger : ValidationRule
  113. {
  114. public int MaxValue { set; get; }
  115. public int MinValue { set; get; }
  116. public ValidateInteger()
  117. {
  118. this.MaxValue = Int32.MaxValue;
  119. this.MinValue = Int32.MinValue;
  120. }
  121. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  122. {
  123. Int32 tempValue;
  124. if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
  125. return new ValidationResult(false, "输入值不能为空");
  126. if (Int32.TryParse(value.ToString(), out tempValue))
  127. {
  128. if (tempValue > this.MinValue && tempValue < this.MaxValue)
  129. {
  130. return new ValidationResult(true, null);
  131. }
  132. else
  133. {
  134. return new ValidationResult(false, string.Format("请输入正确的范围[{0}--{1}]", this.MinValue, this.MaxValue));
  135. }
  136. }
  137. else
  138. {
  139. return new ValidationResult(false, "请输入整数");
  140. }
  141. }
  142. }
  143. public class ValidateDecimal : ValidationRule
  144. {
  145. public double MaxValue { set; get; }
  146. public double MinValue { set; get; }
  147. public ValidateDecimal()
  148. {
  149. this.MaxValue = double.MaxValue;
  150. this.MinValue = double.MinValue;
  151. }
  152. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  153. {
  154. double tempValue;
  155. if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
  156. return new ValidationResult(false, "输入值不能为空");
  157. if (double.TryParse(value.ToString(), out tempValue))
  158. {
  159. if (tempValue >= this.MinValue && tempValue <= this.MaxValue)
  160. {
  161. return new ValidationResult(true, null);
  162. }
  163. else
  164. {
  165. return new ValidationResult(false, string.Format("请输入正确的范围[{0}--{1}]", this.MinValue, this.MaxValue));
  166. }
  167. }
  168. else
  169. {
  170. return new ValidationResult(false, "请输入正确数字");
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// 通用验证
  176. /// </summary>
  177. public class CommonValidate : ValidationRule
  178. {
  179. private Func<object, ValidationResult> m_ValidateFun;
  180. public CommonValidate(Func<object, ValidationResult> validateFun)
  181. {
  182. m_ValidateFun = validateFun;
  183. }
  184. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  185. {
  186. if (m_ValidateFun != null)
  187. {
  188. return m_ValidateFun(value);
  189. }
  190. return new ValidationResult(true, null);
  191. }
  192. }
  193. #endregion
  194. #region 长度验证
  195. public class LengthValidationRule : TValidationRule
  196. {
  197. /// <summary>
  198. /// 限制长度
  199. /// </summary>
  200. public int MaxLength { get; set; }
  201. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  202. {
  203. if (MaxLength <= 0)
  204. {
  205. return new ValidationResult(true, null);
  206. }
  207. if (value == null)
  208. return new ValidationResult(false, ErrorMessage ?? "不能为空值!");
  209. var str = value.ToString();
  210. if (str.Length>MaxLength)
  211. return new ValidationResult(false, ErrorMessage ?? $"字符不能多于{MaxLength}个");
  212. return new ValidationResult(true, null);
  213. }
  214. }
  215. #endregion
  216. }