ValidateUserName.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Globalization;
  6. using System.Windows.Controls;
  7. using System.Text.RegularExpressions;
  8. namespace Aitex.Core.UI.ValidationRules
  9. {
  10. /// <summary>
  11. /// Validate user name
  12. /// </summary>
  13. public class ValidateUserName : ValidationRule
  14. {
  15. /// <summary>
  16. /// Implement the rule that validates user name.
  17. /// </summary>
  18. /// <param name="value"></param>
  19. /// <param name="cultureInfo"></param>
  20. /// <returns></returns>
  21. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  22. {
  23. var userName = (string)value;
  24. // Unicode编码中的汉字范围是/^[\u2E80-\u9FFF]+$/
  25. var pattern = @"[a-zA-Z_\u2E80-\u9FFF]{1}[\w\u2E80-\u9FFF]{0,7}";
  26. var match = Regex.Match(userName, pattern);
  27. if (match.Value.Equals(userName))
  28. {
  29. return new ValidationResult(true, null);
  30. }
  31. return new ValidationResult(false, "Invalid user name");
  32. }
  33. }
  34. }