using System.Text.RegularExpressions; namespace QYZH.InteractiveMagazine.Common.Helpers { /// /// 参数校验工具类 /// public static class ValidateHelper { /// /// 邮箱正则表达式 /// private const string EmailPattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"; /// /// 手机号正则表达式(中国大陆) /// private const string PhonePattern = @"^1[3-9]\d{9}$"; /// /// 身份证号正则表达式(中国大陆) /// private const string IdCardPattern = @"^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$"; /// /// 校验邮箱格式 /// /// 邮箱地址 /// 是否有效 public static bool IsEmail(string email) { if (string.IsNullOrWhiteSpace(email)) { return false; } return Regex.IsMatch(email, EmailPattern); } /// /// 校验手机号格式 /// /// 手机号 /// 是否有效 public static bool IsPhone(string phone) { if (string.IsNullOrWhiteSpace(phone)) { return false; } return Regex.IsMatch(phone, PhonePattern); } /// /// 校验身份证号格式 /// /// 身份证号 /// 是否有效 public static bool IsIdCard(string idCard) { if (string.IsNullOrWhiteSpace(idCard)) { return false; } return Regex.IsMatch(idCard, IdCardPattern); } /// /// 校验URL格式 /// /// URL地址 /// 是否有效 public static bool IsUrl(string url) { if (string.IsNullOrWhiteSpace(url)) { return false; } const string urlPattern = @"^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$"; return Regex.IsMatch(url, urlPattern); } /// /// 校验邮政编码格式 /// /// 邮政编码 /// 是否有效 public static bool IsZipCode(string zipCode) { if (string.IsNullOrWhiteSpace(zipCode)) { return false; } const string zipPattern = @"^\d{6}$"; return Regex.IsMatch(zipCode, zipPattern); } } }