验证中文、英文、电话、手机、邮箱、数字、数字和字母、Url地址和Ip地址的正则表达式
Helper类代码
public class Helper { #region 单列循环 private Helper() { } private static Helper instance = null; /// <summary> /// 静态实例化单体模式 /// 保证应用程序操作某一全局对象,让其保持一致而产生的对象 /// </summary> /// <returns></returns> public static Helper GetInstance() { if (instance == null) { instance = new Helper(); } return instance; } #endregion #region 是否为汉字 /// <summary> /// 验证输入的是否为汉字 /// </summary> /// <param name="input"></param> /// <returns></returns> public bool IsChinese(string input) { return IsMatch(@"^[\u4e00-\u9fa5]+$", input); } #endregion #region 是否为手机号码 /// <summary> /// 验证是否为手机号码 /// </summary> /// <param name="mobile">输入的手机号码</param> /// <returns></returns> public bool IsMobile(string mobile) { string reg = "^1[3|4|5|7|8][0-9]\\d{8}$"; return IsMatch(reg, mobile); } #endregion #region 是否为电话号码 /// <summary> /// 验证是否为电话号码 /// </summary> /// <param name="phone">电话号码</param> /// <returns></returns> public bool IsPhone(string phone) { string regex = "^(\\d{3,4}-)\\d{7,8}$"; return IsMatch(regex, phone); } #endregion #region 是否为数字 /// <summary> /// 是否为数字 /// </summary> /// <param name="number"></param> /// <returns></returns> private bool IsNumber(string number) { string regex = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$"; return IsMatch(regex, number); } #endregion #region 匹配非负整数 /// <summary> /// 匹配非负整数 /// </summary> /// <param name="input"></param> /// <returns></returns> public bool IsNotNagtive(string input) { return IsMatch(@"^\d+$", input); } #endregion #region 匹配正整数 /// <summary> /// 匹配正整数 /// </summary> /// <param name="input"></param> /// <returns></returns> public bool IsUnit(string input) { return IsMatch(@"^[0-9]*[1-9][0-9]*$", input); } #endregion #region 字符是否包含英文 /// <summary> /// 判断输入字符是否包含英文 /// </summary> /// <param name="input"></param> /// <returns></returns> public bool IsHaveEnglish(string input) { return IsMatch(@"^[A-Za-z]+$", input); } #endregion #region 是否为Email /// <summary> /// 是否为Email /// </summary> /// <param name="mail"></param> /// <returns></returns> public bool IsEmail(string mail) { string regex = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; return IsMatch(regex, mail); } #endregion #region 判断是否包含数字和英文字母 /// <summary> /// 判断是否包含数字和英文字母 /// </summary> /// <param name="input"></param> /// <returns></returns> public bool IsNumAndEng(string input) { return IsMatch(@"^[A-Za-z0-9]+$", input); } #endregion #region 判断是否为url /// <summary> /// 判断是否为超链接 /// </summary> /// <param name="url"></param> /// <returns></returns> public bool IsUrl(string url) { string regex = @"[a-zA-z]+://[^\s]*"; return IsMatch(regex, url); } #endregion #region 判断输入的字符串是否是一个合法IP地址 /// <summary> /// 判断输入的字符串是否是一个合法IP地址 /// </summary> /// <param name="ip"></param> /// <returns></returns> public bool IsIPV4(string ip) { string[] ips = ip.Split(','); for (int i = 0; i < ips.Length; i++) { if (!IsMatch(@"^\d+$", ips[i])) { return false; } if (Convert.ToUInt16(ips[i]) > 255) { return false; } } return true; } #endregion #region 判断输入的字符串是否是一个合法的IPV6地址 /// <summary> /// 判断输入的字符串是否是一个合法的IPV6地址 /// </summary> /// <param name="ip"></param> /// <returns></returns> public bool IsIPV6(string ip) { string pattern = ""; string temp = ip; string[] strs = temp.Split(':'); if (strs.Length > 8) { return false; } int count = Helper.GetStringCount(ip, "::"); if (count > 1) { return false; } else if (count == 0) { pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$"; return IsMatch(pattern, ip); } else { pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$"; return IsMatch(pattern, ip); } } #endregion /// <summary> /// 调用Regex中IsMatch函数实现一般的正则表达式匹配 /// </summary> /// <param name="pattern">要匹配的正则表达式模式。</param> /// <param name="input">要搜索匹配项的字符串</param> /// <returns>如果正则表达式找到匹配项,则为 true;否则,为 false。</returns> public static bool IsMatch(string pattern, string input) { if (input == null || input == "") return false; Regex regex = new Regex(pattern); return regex.IsMatch(input); } /// <summary> /// 从输入字符串的第一个字符开始,用替换字符串替换指定的正则表达式模式的所有匹配项 /// </summary> /// <param name="pattern">模式字符串</param> /// <param name="input">输入字符串</param> /// <param name="replacement">用于替换的字符串</param> /// <returns>返回替换后的结果</returns> public static string Replace(string pattern, string input, string replacement) { Regex regex = new Regex(pattern); return regex.Replace(input, replacement); } /// <summary> /// 在由正则表达式模式定义的位置拆分输入字符串 /// </summary> /// <param name="pattern">模式字符串</param> /// <param name="input">输入字符串</param> /// <returns></returns> public static string[] Split(string pattern, string input) { Regex regex = new Regex(pattern); return regex.Split(input); } /// <summary> /// 判断compare在input中出现的次数 /// </summary> /// <param name="input">源字符串</param> /// <param name="compare">用于比较的字符串</param> /// <returns>字符串compare在input中出现的次数</returns> public static int GetStringCount(string input, string compare) { int index = input.IndexOf(compare); if (index != -1) { return 1 + GetStringCount(input.Substring(index + compare.Length), compare); } else { return 0; } } //private static string GetRegularFormula(string RegulatFormula) //{ // string str = ""; // if (RegulatFormula.ToUpper().IndexOf("RegularFormula".ToUpper()) > -1) // { // RegularFormula formula = new RegularFormula(); // } //} }
测试代码
protected void btnVerification_Click(object sender, EventArgs e) { string chinese = txtChinese.Text.Trim(); string english = txtEnglish.Text.Trim(); string numEng = txtNumAndEng.Text.Trim(); string num = txtInteger.Text.Trim(); string mobile = txtMobile.Text.Trim(); string phone = txtPhome.Text.Trim(); string emil = txtMail.Text.Trim(); string url = txtUrl.Text.Trim(); if (Helper.GetInstance().IsChinese(chinese)) { Response.Write("输入的中文正确:" + chinese + "<br/>"); } else { Response.Write("请输入全中文<br/>"); } if (Helper.GetInstance().IsHaveEnglish(english)) { Response.Write("输入的为英文字符串:" + english + "<br/>"); } else { Response.Write("请输入全英文<br/>"); } if (Helper.GetInstance().IsNumAndEng(numEng)) { Response.Write("数字字母混合:" + numEng + "<br/>"); } else { Response.Write("不是正确的数字字母混合<br/>"); } if (Helper.GetInstance().IsUnit(num)) { Response.Write("输入的为正整数:" + num + "<br/>"); } else if (Helper.GetInstance().IsNotNagtive(num)) { Response.Write("输入的为负整数:" + num + "<br/>"); } else { Response.Write("数字输入有误<br/>"); } if (Helper.GetInstance().IsMobile(mobile)) { Response.Write("手机号码为:" + mobile + "<br/>"); } else { Response.Write("手机号码不正确<br/>"); } if (Helper.GetInstance().IsPhone(phone)) { Response.Write("电话号码为:" + phone + "<br/>"); } else { Response.Write("电话号码有误<br/>"); } if (Helper.GetInstance().IsEmail(emil)) { Response.Write("邮箱地址为:" + emil + "<br/>"); } else { Response.Write("邮箱地址有误<br/>"); } if (Helper.GetInstance().IsUrl(url)) { Response.Write("url地址为:" + url + "<br/>"); } else { Response.Write("请输入正确的url地址"); } }
结果图如下:
不忘初心,方得始终