61 lines
1.7 KiB
C#
61 lines
1.7 KiB
C#
using System.Text.RegularExpressions;
|
||
|
||
namespace Atomx.Utils.Extension
|
||
{
|
||
public class RegexExtension
|
||
{
|
||
/// <summary>
|
||
/// 判断是否是电子邮件格式
|
||
/// </summary>
|
||
/// <param name="email"></param>
|
||
/// <returns></returns>
|
||
public static bool IsEmail(string email)
|
||
{
|
||
return Regex.IsMatch(email, @"^\S+@\S+\.\S+$");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断手机号码是否符合规则,不考虑国家代码的情况下
|
||
/// </summary>
|
||
/// <param name="mobile"></param>
|
||
/// <returns></returns>
|
||
public static bool IsValidMobile(string mobile)
|
||
{
|
||
string pattern = "^\\d{7,15}$";
|
||
return Regex.IsMatch(mobile, pattern);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断手机号码是否符合规则,不考虑国家代码的情况下
|
||
/// </summary>
|
||
/// <param name="mobile"></param>
|
||
/// <returns></returns>
|
||
public static bool IsValidGlobalMobile(string mobile)
|
||
{
|
||
// 正则表达式规则解释:
|
||
// ^ 开始
|
||
// \\+ 匹配一个加号 (E.164要求号码以加号开始)
|
||
// \\d{1,3} 匹配1到3位数字,即国家代码
|
||
// \\d{7,15} 匹配7到15位数字,即电话号码
|
||
// $ 结束
|
||
string pattern = "^\\+\\d{1,3}\\d{7,15}$";
|
||
return Regex.IsMatch(mobile, pattern);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 验证账号是否是邮箱地址或者手机号
|
||
/// </summary>
|
||
/// <param name="account"></param>
|
||
/// <returns></returns>
|
||
public static bool IsValidAccount(string account)
|
||
{
|
||
string pattern = @"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
|
||
if (Regex.IsMatch(account, pattern))
|
||
{
|
||
return true;
|
||
}
|
||
return IsValidMobile(account);
|
||
}
|
||
}
|
||
}
|