killapp/Assets/Scripts/Utils/ValidationUtils.cs
2026-04-16 14:57:19 +08:00

70 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Text.RegularExpressions;
namespace Kill.Utils
{
/// <summary>
/// 验证工具类
/// 提供常用的数据验证方法
/// </summary>
public static class ValidationUtils
{
#region
/// <summary>
/// 验证邮箱格式是否有效(正则验证,更严格)
/// </summary>
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
/// <summary>
/// 验证简单密码格式6位数字
/// </summary>
public static bool IsValidSimplePassword(string password)
{
if (string.IsNullOrEmpty(password))
return false;
// 6位数字
string pattern = @"^\d{6}$";
return Regex.IsMatch(password, pattern);
}
/// <summary>
/// 验证复杂密码格式6-16位同时包含数字、字母和特殊字符
/// </summary>
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
}
}