UsernameValidationRule.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. using System.Xml.Linq;
  10. namespace TeamAAS_VP.ValidationRules
  11. {
  12. public class UsernameValidationRule : ValidationRule
  13. {
  14. // 最小长度和最大长度
  15. public int MinLength { get; set; } = 1;
  16. public int MaxLength { get; set; } = 32;
  17. public bool AllowEmpty { get; set; } = false;
  18. // 验证方法
  19. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  20. {
  21. string input = value as string;
  22. if (string.IsNullOrWhiteSpace(input) && !AllowEmpty)
  23. return new ValidationResult(false, "Name cannot be empty.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  24. else if (string.IsNullOrWhiteSpace(input) && AllowEmpty)
  25. {
  26. return ValidationResult.ValidResult;
  27. }
  28. if (input.StartsWith(" ") || input.StartsWith("-") || input.StartsWith("_"))
  29. {
  30. return new ValidationResult(false, "The name cannot start with the characters \" \", \"_\" or \"-\".".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  31. }
  32. string msg = "Name must be between {MinLength} and {MaxLength} characters.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t");
  33. // 修复:计算字符串的实际长度,将汉字视为两个字符
  34. int actualLength = input.Length;//input.Sum(c => c > 127 ? 2 : 1);
  35. if (actualLength < MinLength || actualLength > MaxLength)
  36. return new ValidationResult(false, msg.Replace("{MinLength}", MinLength.ToString()).Replace("{MaxLength}", MaxLength.ToString()));
  37. //if (!Regex.IsMatch(input, @"^[\u4e00-\u9fa5\u3040-\u309F\u30A0-\u30FFa-zA-Z0-9_ -]{1,32}$"))
  38. // return new ValidationResult(false, App.Current.Resources["ValidationRules.Name.Hint3"] as string);
  39. //@"^[\p{L}\p{M}0-9_ -]{1,32}$"
  40. if (!Regex.IsMatch(input, @"^(?:[\p{L}\p{M}0-9_ -]|[\uFF00-\uFFFF]|[^\x00-\xFF]){1,32}$", RegexOptions.Compiled))
  41. return new ValidationResult(false, "Name contains invalid characters.".Replace("\\n", Environment.NewLine).Replace("\\t", "\t"));
  42. // 如果所有检查通过,返回验证成功
  43. return ValidationResult.ValidResult;
  44. }
  45. public static bool IsValidCharacter(string input)
  46. {
  47. foreach (char c in input)
  48. {
  49. // 获取字符的 Unicode 码点
  50. int codePoint = (int)c;
  51. // 检查是否属于常见的汉字区、扩展区、补充平面等范围
  52. if ((codePoint >= 0x4E00 && codePoint <= 0x9FFF) || // 基本汉字区 (U+4E00 - U+9FFF)
  53. (codePoint >= 0x3400 && codePoint <= 0x4DBF) || // 扩展A区 (U+3400 - U+4DBF)
  54. (codePoint >= 0x20000 && codePoint <= 0x2A6DF) || // 扩展B区 (U+20000 - U+2A6DF)
  55. (codePoint >= 0x2A700 && codePoint <= 0x2B73F) || // 扩展C区 (U+2A700 - U+2B73F)
  56. (codePoint >= 0x2B740 && codePoint <= 0x2B81F) || // 扩展D区 (U+2B740 - U+2B81F)
  57. (codePoint >= 0x2B820 && codePoint <= 0x2CEAF) || // 扩展E区 (U+2B820 - U+2CEAF)
  58. (codePoint >= 0x2CEB0 && codePoint <= 0x2EBEF) || // 扩展F区 (U+2CEB0 - U+2EBEF)
  59. (codePoint >= 0x3040 && codePoint <= 0x309F) || // 平假名区 (U+3040 - U+309F)
  60. (codePoint >= 0x30A0 && codePoint <= 0x30FF)) // 片假名区 (U+30A0 - U+30FF)
  61. {
  62. continue; // 如果字符在合法的 Unicode 区域内,继续检查下一个字符
  63. }
  64. else
  65. {
  66. return false; // 如果字符不在合法范围内,则返回 false
  67. }
  68. }
  69. // 如果都没匹配,返回false
  70. return false;
  71. }
  72. }
  73. }