namespace GDNXFD.Alert.Config.Validations { using Data.Repositories; using Data; using System; using System.Windows.Controls; using System.Text.RegularExpressions; /// /// Rule for required fields /// public class AlertRuleIDValidationRule : ValidationRule { public static bool Enabled = true; /// /// When overridden in a derived class, performs validation checks on a value. /// /// The value from the binding target to check. /// The culture to use in this rule. /// /// A object. /// public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { if (!Enabled) return new ValidationResult(true, null); if (value == null || String.IsNullOrWhiteSpace(value.ToString())) return new ValidationResult(false, "不能为空!"); string id = value.ToString(); if (id.Length < 5) return new ValidationResult(false, "至少输入5个字母或数字!"); if (!IsNatural_Number(id)) return new ValidationResult(false, "只能输入字母或数字,不能使用特殊字符!"); AlertRule ar = AlertRuleRepository.GetAlertRuleById(id); if (ar != null) return new ValidationResult(false, "编码已存在,请重新输入!"); return new ValidationResult(true, null); } private bool IsNatural_Number(string strNum) { Regex notWholePattern = new Regex(@"^[0-9a-zA-Z\$]+$"); return notWholePattern.IsMatch(strNum, 0); } } }