123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- namespace FWindSoft.Wpf
- {
- /*
- DataErrorValidationRule,ExceptionValidationRule
- */
- public class TValidationRule: ValidationRule
- {
- /// <summary>
- /// 错误信息
- /// </summary>
- public string ErrorMessage { get; set; }
- public override ValidationResult Validate(object value, CultureInfo cultureInfo)
- {
- if (value == null)
- return new ValidationResult(false, ErrorMessage??"不能为空值!");
-
- if (string.IsNullOrWhiteSpace(value.ToString()))
- return new ValidationResult(false, ErrorMessage ?? "不能为空字符串!");
- return new ValidationResult(true, null);
- }
- }
- public class RequireValidationRule : TValidationRule
- {
- public override ValidationResult Validate(object value, CultureInfo cultureInfo)
- {
- if (value == null)
- return new ValidationResult(false, ErrorMessage ?? "不能为空值!");
- if (string.IsNullOrWhiteSpace(value.ToString()))
- return new ValidationResult(false, ErrorMessage ?? "不能为空字符串!");
- return new ValidationResult(true, null);
- }
- }
- #region 正则表达式判定
- /// <summary>
- /// 这则表达式验证基类
- /// </summary>
- public class RegexValidationRule : TValidationRule
- {
- protected string m_Match;
- public override ValidationResult Validate(object value, CultureInfo cultureInfo)
- {
- if (value==null||string.IsNullOrWhiteSpace(value.ToString()))
- return new ValidationResult(true, null);
- if (m_Match == null)
- return new ValidationResult(true, null);
- if (!string.IsNullOrWhiteSpace(value.ToString()))
- {
- Regex tempMatch = new Regex(m_Match);
- if (!tempMatch.IsMatch(value.ToString()))
- {
- return new ValidationResult(false, ErrorMessage ?? "信息错误");
- }
- }
- return new ValidationResult(true, null);
- }
- }
- public class EMailValidationRule : RegexValidationRule
- {
- public EMailValidationRule()
- {
- m_Match = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
- }
- }
- public class FaxValidationRule : RegexValidationRule
- {
- public FaxValidationRule()
- {
- m_Match = @"^(\d{3,4}-?)?\d{7,8}$";
- }
- }
- public class PhoneValidationRule : RegexValidationRule
- {
- public PhoneValidationRule()
- {
- m_Match = @"^(\d{3,4}-?)?\d{7,8}$";
- }
- }
- public class CustomRegexValidationRule : RegexValidationRule
- {
- public CustomRegexValidationRule()
- {
- }
- public string Match
- {
- get { return this.m_Match; }
- set { this.Match = value; }
- }
- }
- #endregion
- #region 使用委托判定
- public class MethodValidationRule : TValidationRule
- {
- public override ValidationResult Validate(object value, CultureInfo cultureInfo)
- {
- //if (value == null)
- // return new ValidationResult(false, ErrorMessage ?? "不能为空值!");
- if(Method!=null)
- return Method?.Invoke(value);
-
- return new ValidationResult(true, null);
- }
- public Func<object, ValidationResult> Method { get; set; }
- }
- #endregion
- #region 特殊判定
- public class ValidateInteger : ValidationRule
- {
- public int MaxValue { set; get; }
- public int MinValue { set; get; }
- public ValidateInteger()
- {
- this.MaxValue = Int32.MaxValue;
- this.MinValue = Int32.MinValue;
- }
- public override ValidationResult Validate(object value, CultureInfo cultureInfo)
- {
- Int32 tempValue;
- if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
- return new ValidationResult(false, "输入值不能为空");
- if (Int32.TryParse(value.ToString(), out tempValue))
- {
- if (tempValue > this.MinValue && tempValue < this.MaxValue)
- {
- return new ValidationResult(true, null);
- }
- else
- {
- return new ValidationResult(false, string.Format("请输入正确的范围[{0}--{1}]", this.MinValue, this.MaxValue));
- }
- }
- else
- {
- return new ValidationResult(false, "请输入整数");
- }
- }
- }
- public class ValidateDecimal : ValidationRule
- {
- public double MaxValue { set; get; }
- public double MinValue { set; get; }
- public ValidateDecimal()
- {
- this.MaxValue = double.MaxValue;
- this.MinValue = double.MinValue;
- }
- public override ValidationResult Validate(object value, CultureInfo cultureInfo)
- {
- double tempValue;
- if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
- return new ValidationResult(false, "输入值不能为空");
- if (double.TryParse(value.ToString(), out tempValue))
- {
- if (tempValue >= this.MinValue && tempValue <= this.MaxValue)
- {
- return new ValidationResult(true, null);
- }
- else
- {
- return new ValidationResult(false, string.Format("请输入正确的范围[{0}--{1}]", this.MinValue, this.MaxValue));
- }
- }
- else
- {
- return new ValidationResult(false, "请输入正确数字");
- }
- }
- }
- /// <summary>
- /// 通用验证
- /// </summary>
- public class CommonValidate : ValidationRule
- {
- private Func<object, ValidationResult> m_ValidateFun;
- public CommonValidate(Func<object, ValidationResult> validateFun)
- {
- m_ValidateFun = validateFun;
- }
- public override ValidationResult Validate(object value, CultureInfo cultureInfo)
- {
- if (m_ValidateFun != null)
- {
- return m_ValidateFun(value);
- }
- return new ValidationResult(true, null);
- }
- }
- #endregion
- #region 长度验证
- public class LengthValidationRule : TValidationRule
- {
- /// <summary>
- /// 限制长度
- /// </summary>
- public int MaxLength { get; set; }
- public override ValidationResult Validate(object value, CultureInfo cultureInfo)
- {
- if (MaxLength <= 0)
- {
- return new ValidationResult(true, null);
- }
- if (value == null)
- return new ValidationResult(false, ErrorMessage ?? "不能为空值!");
- var str = value.ToString();
- if (str.Length>MaxLength)
- return new ValidationResult(false, ErrorMessage ?? $"字符不能多于{MaxLength}个");
- return new ValidationResult(true, null);
- }
- }
- #endregion
- }
|