using System; using System.Text; using System.Text.RegularExpressions; namespace HuizhongLibrary.RegularExpressions { public class DataRegex { //验证日期格式 public static bool IsDateTime(string value) { if (value == "") return true; try { DateTime dt = Convert.ToDateTime(value); if (dt > new DateTime(3000, 1, 1) || dt < new DateTime(1900, 1, 1)) return false; } catch { return false; } return true; } /// /// 验证日期格式 /// /// /// {0格式正确}{1空值}{2日期格式错误}{3日期超出范围1900-1-1至3000-1-1} public static int IsDate(string value) { if (value == "") return 1; DateTime dt; try { dt = Convert.ToDateTime(value); } catch { return 2; } if (dt > new DateTime(3000, 1, 1) || dt < new DateTime(1900, 1, 1)) return 3; return 0; } /// /// 验证是否是整数 /// /// /// 验证是否可为负,true验证不能为负,flase可为负 /// public static bool IsNumberInteger(string value, bool IsMinus) { if (value == "") return true; try { if (IsMinus == false) { Convert.ToInt32(value); } else { if (Convert.ToInt32(value) < 0) return false; } } catch { return false; } return true; } /// /// 验证是否是整数 /// /// /// public static bool IsNumberInteger(string value, int minNumber, int maxNumber) { if (value == "") return true; int sl = 0; try { sl = Convert.ToInt32(value); if (sl < minNumber || sl > maxNumber) return false; } catch { return false; } return true; } /// /// 验证是否是整数 /// public static bool IsNumberInteger(string value) { if (value == "") return true; try { Convert.ToInt32(value); } catch { return false; } return true; } /// /// 验证是否是浮点数 /// public static bool IsNumberDouble(string value, Double minNumber, Double maxNumber) { if (value == "") return true; double sl = 0; try { sl = Convert.ToDouble(value); if (sl < minNumber || sl > maxNumber) return false; } catch { return false; } return true; } /// /// 验证是否是浮点数 /// public static bool IsNumberDouble(string value) { if (value == "") return true; try { Convert.ToDouble(value); } catch { return false; } return true; } /// /// 验证是否是浮点数,并且小数位不能超过N位 /// public static bool IsNumberDouble(string value, int len) { if (value == "") return true; try { Match mc = Regex.Match(value, @"\d{1,9}$|\d{1,9}[.]\d{1," + len.ToString() + "}$"); if (value != mc.Value) return false;//如果找到的字符串不等于原字符串那么说明是错误的格式 } catch { return false; } return true; } /// /// 验证Email地址 /// public static bool IsEmail(string value) { if (value == "") return true; if (Regex.Matches(value, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$").Count == 0) return false; return true; } /// /// 验证身份证号码 /// public static bool IsIdentityCard(string value) { if (value == "") return true; if (Regex.Matches(value, @"^\d{15}$|^\d{18}$|^\d{17}(\d|X|x)$").Count == 0) return false; return true; } /// /// 验证邮政编码 /// public static bool IsPostalcode(string value) { if (value == "") return true; if (Regex.Matches(value, @"^\d{6}$").Count == 0) return false; return true; } /// /// 验证电话号码 /// public static bool IsPhone(string value) { if (value == "") return true; if (Regex.Matches(value, @"^^\d{11}$|\d{8}$|0\d{2,3}-?\d{8}$").Count == 0) return false; return true; } /// /// 验证IP地址 /// public static bool IsIP(string value) { if (value == "") return true; string[] ss = value.Split('.'); if (ss.Length != 4) return false; foreach (string s in ss) { if (DataRegex.IsNumberInteger(s, 0, 255) == false) return false; } return true; } /// /// 验证Url地址 /// public static bool IsUrlAddress(string value) { if (value == "") return true; if (Regex.Matches(value, @"^http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$").Count == 0) return false; return true; } /// /// 验证时间 /// public static bool IsTime(string value) { if (value == "") return true; if (Regex.Matches(value, @"^[0-1][0-9]:[0-5][0-9]:[0-5][0-9]$|^2[0-3]:[0-5][0-9]:[0-5][0-9]$").Count == 0) return false; return true; } } }