c# 字符串验证(邮箱、电话、数字、ip、身份证等)
2016-09-14 09:52 newbirth 阅读(668) 评论(0) 编辑 收藏 举报using System; using System.Text.RegularExpressions; namespace HuaTong.General.Utility { /// <summary> /// 字符串验证 /// </summary> public static class StringValidate { private static Regex RegNumeric = new Regex("^[0-9]+$"); private static Regex RegNumericSign = new Regex("^[+-]?[0-9]+$"); private static Regex RegDecimal = new Regex("^([0-9]+[.]?[0-9]+)|([0-9]+)$"); private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); private static Regex RegEmail = new Regex(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); private static Regex RegChina = new Regex("^[\u4e00-\u9fa5]*$"); private static Regex RegPostcode = new Regex(@"^(\d{6})$"); private static Regex RegUrl = new Regex(@"^(http|https|ftp|mms)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$"); private static Regex RegTEL = new Regex(@"^\d{6,8}|\d{3,4}-\d{6,8}$"); private static Regex RegDate = new Regex(@"^\d{4}(\-|\/|\.)\d{1,2}(\-|\/|\.)\d{1,2}$"); private static Regex RegMobile = new Regex(@"^1(3|4|5|7|8|9)\d{9}$"); private static Regex RegUserName = new Regex("^([\u4E00-\u9FA5a-zA-Z0-9_-]){2,16}$"); private static Regex RegIP = new Regex(@"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); private static Regex RegIdCard = new Regex(@"^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)(\d{13}|\d{15}[\dxX])$"); /// <summary> /// 自定义验证 /// </summary> public static bool IsMatch(string value, string partten) { if (!string.IsNullOrEmpty(value)) { Regex reg = new Regex(partten); Match m = reg.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否数字 /// </summary> public static bool IsNumeric(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegNumeric.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否数字 带正负号 /// </summary> public static bool IsNumberSign(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegNumericSign.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否是小数 /// </summary> public static bool IsDecimal(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegDecimal.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否是小数 带正负号 /// </summary> public static bool IsDecimalSign(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegDecimalSign.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否中文 /// </summary> public static bool IsHasCHZN(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegChina.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否是Email /// </summary> public static bool IsEmail(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegEmail.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否是邮政编码 /// </summary> public static bool IsPostcode(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegPostcode.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否是URL /// </summary> public static bool IsUrl(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegUrl.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否日期 /// </summary> /// <returns></returns> public static bool IsDate(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegDate.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否日期时间 /// </summary> public static bool IsDatetime(string value) { DateTime time; return DateTime.TryParse(value, out time); } /// <summary> /// 是否为合法的电话号码 /// </summary> public static bool IsTel(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegTEL.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否为合法的手机号码 /// </summary> public static bool IsMobile(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegMobile.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否为合法的用户名 限中文/英文/数字/减号/下划线 /// </summary> public static bool IsUserName(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegUserName.Match(value); return m.Success; } else { return false; } } /// <summary> /// 是否为合法的IPv4地址 /// </summary> public static bool IsIPAddress(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegIP.Match(value); return m.Success; } else { return false; } } /// <summary> /// 判断对象是否为空 /// </summary> public static bool IsNullOrEmpty(object value) { if (value == null) { return true; } if (value.GetType() == typeof(String)) { if (string.IsNullOrEmpty(value.ToString().Trim())) { return true; } } if (value.GetType() == typeof(DBNull)) { return true; } //不为空 return false; } /// <summary> /// 是否合法身份证号 /// </summary> public static bool IsIdCard(string value) { if (!string.IsNullOrEmpty(value)) { Match m = RegIdCard.Match(value); return m.Success; } else { return false; } } } }