using System; using System.Text.RegularExpressions; namespace Kill.Utils { /// /// 验证工具类 /// 提供常用的数据验证方法 /// public static class ValidationUtils { #region 邮箱验证 /// /// 验证邮箱格式是否有效(正则验证,更严格) /// public static bool IsValidEmailStrict(string email) { if (string.IsNullOrEmpty(email)) return false; // RFC 5322 标准邮箱正则 string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"; return Regex.IsMatch(email, pattern); } #endregion #region 密码验证 /// /// 验证简单密码格式(6位数字) /// public static bool IsValidSimplePassword(string password) { if (string.IsNullOrEmpty(password)) return false; // 6位数字 string pattern = @"^\d{6}$"; return Regex.IsMatch(password, pattern); } /// /// 验证复杂密码格式(6-16位,同时包含数字、字母和特殊字符) /// public static bool IsValidComplexPassword(string password) { if (string.IsNullOrEmpty(password)) return false; // 长度6-16位 if (password.Length < 6 || password.Length > 16) return false; // 必须包含数字 bool hasDigit = Regex.IsMatch(password, @"\d"); // 必须包含字母 bool hasLetter = Regex.IsMatch(password, @"[a-zA-Z]"); // 必须包含特殊字符 bool hasSpecial = Regex.IsMatch(password, @"[!@#$%^&*()_+\-=\[\]{};':""\\|,.<>\/?]"); return hasDigit && hasLetter && hasSpecial; } #endregion } }