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