AlertRuleIDValidationRule.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. namespace GDNXFD.Alert.Config.Validations
  2. {
  3. using Data.Repositories;
  4. using Data;
  5. using System;
  6. using System.Windows.Controls;
  7. using System.Text.RegularExpressions;
  8. /// <summary>
  9. /// Rule for required fields
  10. /// </summary>
  11. public class AlertRuleIDValidationRule : ValidationRule
  12. {
  13. public static bool Enabled = true;
  14. /// <summary>
  15. /// When overridden in a derived class, performs validation checks on a value.
  16. /// </summary>
  17. /// <param name="value">The value from the binding target to check.</param>
  18. /// <param name="cultureInfo">The culture to use in this rule.</param>
  19. /// <returns>
  20. /// A <see cref="T:System.Windows.Controls.ValidationResult"/> object.
  21. /// </returns>
  22. public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
  23. {
  24. if (!Enabled)
  25. return new ValidationResult(true, null);
  26. if (value == null || String.IsNullOrWhiteSpace(value.ToString()))
  27. return new ValidationResult(false, "不能为空!");
  28. string id = value.ToString();
  29. if (id.Length < 5)
  30. return new ValidationResult(false, "至少输入5个字母或数字!");
  31. if (!IsNatural_Number(id))
  32. return new ValidationResult(false, "只能输入字母或数字,不能使用特殊字符!");
  33. AlertRule ar = AlertRuleRepository.GetAlertRuleById(id);
  34. if (ar != null)
  35. return new ValidationResult(false, "编码已存在,请重新输入!");
  36. return new ValidationResult(true, null);
  37. }
  38. private bool IsNatural_Number(string strNum)
  39. {
  40. Regex notWholePattern = new Regex(@"^[0-9a-zA-Z\$]+$");
  41. return notWholePattern.IsMatch(strNum, 0);
  42. }
  43. }
  44. }