using System.Text.RegularExpressions; namespace Common { public class DataValidate { private static Regex RegNumber = new Regex("^[0-9]+$"); private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$"); private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$"); private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$"); private static Regex RegCHZN = new Regex("[一-龥]"); protected DataValidate() { } public static bool IsAreaCode(string input) { return DataValidate.IsNumber(input) && input.Length >= 3 && input.Length <= 5; } public static bool IsDecimal(string input) { if (string.IsNullOrEmpty(input)) return false; return Regex.IsMatch(input, "^[0-9]+[.]?[0-9]+$"); } public static bool IsDecimalSign(string input) { if (string.IsNullOrEmpty(input)) return false; return Regex.IsMatch(input, "^[+-]?[0-9]+[.]?[0-9]+$"); } public static bool IsEmail(string strEmail) { if (string.IsNullOrEmpty(strEmail)) return false; return Regex.IsMatch(strEmail, "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"); } public static bool IsIP(string strIp) { return !string.IsNullOrEmpty(strIp) && Regex.IsMatch(strIp.Trim(), "^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$"); } public static bool IsNumber(string strNumber) { return Regex.IsMatch(strNumber, "^[0-9]+$"); } public static bool IsNumberSign(string input) { if (string.IsNullOrEmpty(input)) return false; return Regex.IsMatch(input, "^[+-]?[0-9]+$"); } public static bool IsPostCode(string postCode) { return Regex.IsMatch(postCode, "^\\d{6}$"); } public static bool IsMobile(string mobile) { return Regex.IsMatch(mobile, "^1[3|4|5|8]\\d{9}$") || Regex.IsMatch(mobile, "^0(([1,2]\\d)|([3-9]\\d{2}))\\d{8}$"); } public static bool IsExistMobile(string mobile, string mobiles) { return new Regex(mobile).Match(mobiles).Success; } public static bool IsIdentityNumber(string num) { return Regex.IsMatch(num, "^\\d{17}[\\d|X]|\\d{15}$"); } public static bool IsTime(string timeval) { return Regex.IsMatch(timeval, "20\\d{2}\\-[0-1]{1,2}\\-[0-3]?[0-9]?(\\s*((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?))?"); } public static bool IsURL(string url) { if (string.IsNullOrEmpty(url)) return false; return Regex.IsMatch(url, "^http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?$"); } public static bool IsPhoneNumber(string phoneNumber) { return Regex.IsMatch(phoneNumber, "^(\\(\\d{3}\\)|\\d{3}-)?\\d{7,8}$"); } public static bool IsValidId(string input) { if (string.IsNullOrEmpty(input)) return false; input = input.Replace("|", "").Replace(",", "").Replace("-", "").Replace(" ", "").Trim(); if (string.IsNullOrEmpty(input)) return false; return DataValidate.IsNumber(input); } public static bool IsValidUserName(string userName, out string msg) { userName = userName.Trim(); msg = ""; if (string.IsNullOrEmpty(userName)) { msg = "用户名不能为空"; return false; } if (StringHelper.GetStringLength(userName) > 20) { msg = "用户名长度不能大于20"; return false; } if (StringHelper.GetStringLength(userName) < 4) { msg = "用户名长度不能小于4"; return false; } string str = "\\/\"[]:|<>+=;,?*@."; for (int index = 0; index < userName.Length; ++index) { if (str.IndexOf(userName[index]) >= 0) { msg = "用户名含有非法字符"; return false; } } return true; } public static bool IsTooShortPassword(string password) { return StringHelper.GetStringLength(password.Trim()) < 6; } public static bool IsHasCHZN(string inputData) { return DataValidate.RegCHZN.Match(inputData).Success; } } }