using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace QM.Common { /// /// 模块编号:QM.Common.SysValidate /// 作 用:共通校验方法 /// 作 者: /// 编写日期:2014-05-05 /// public class SysValidate { #region 变量 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]+$"); //等价于^[+-]?\d+[.]?\d+$ private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); private static Regex RegIP = new Regex("^((25[0-5]|2[0-4]\\d|1?\\d{1,2})\\.){3}(25[0-5]|2[0-4]\\d|1?\\d{1,2})$ "); #endregion #region 数值校验 /// /// 判断整型数字 /// /// 需要校验的数值 /// /// 参数为数字类型返回ture,否则返回false。 /// public static bool IsInt(string pIntValue) { #region try { int intTemp = Convert.ToInt32(pIntValue); return true; } catch { return false; } #endregion } /// /// 是否数字字符串 /// /// 输入数值字符串 /// 参数为数字类型返回ture,否则返回false public static bool IsNumeric(string pValue) { #region Match m = RegNumber.Match(pValue); return m.Success; #endregion } /// /// 是否数字字符串可带正负号 /// /// 输入数值字符串 /// 参数为数字类型返回ture,否则返回false public static bool IsNumericSign(string pValue) { #region Match m = RegNumberSign.Match(pValue); return m.Success; #endregion } /// /// 是否是浮点数 /// /// 输入浮点数值字符串 /// 参数为浮点数类型返回ture,否则返回false public static bool IsDecimal(string pValue) { #region Match m = RegDecimal.Match(pValue); return m.Success; #endregion } /// /// 是否是浮点数可带正负号 /// /// 输入浮点数值字符串 /// 参数为浮点数型返回ture,否则返回false public static bool IsDecimalSign(string pValue) { #region Match m = RegDecimalSign.Match(pValue); return m.Success; #endregion } #endregion #region 字符校验 /// /// 是否英文字母字符串 /// /// 输入数值字符串 /// 是返回ture,否则返回false public static bool IsEnString(string pValue) { #region for (int intCnt = 0; intCnt < pValue.Length; intCnt++) { int intASC = (int)System.Text.Encoding.ASCII.GetBytes(FuncString.GetSubString(pValue, intCnt, 1))[0]; if (intASC > 65 || intASC > 122) { return false; } } return true; #endregion } #endregion #region 日期校验 /// 是否日期 /// 输入字符串 /// true/false public static bool IsDate(string pInpDate) { #region string datestr = pInpDate; string year, month, day; string[] c ={ "/", "-", "." }; string cs = ""; for (int i = 0; i < c.Length; i++) { if (datestr.IndexOf(c[i]) > 0) { cs = c[i]; break; } }; if (cs != "") { year = datestr.Substring(0, datestr.IndexOf(cs)); if (year.Length != 4) { return false; }; datestr = datestr.Substring(datestr.IndexOf(cs) + 1); month = datestr.Substring(0, datestr.IndexOf(cs)); if ((month.Length != 2) || (Convert.ToInt16(month) > 12)) { return false; }; datestr = datestr.Substring(datestr.IndexOf(cs) + 1); day = datestr; if ((day.Length != 2) || (Convert.ToInt16(day) > 31)) { return false; }; return CheckDatePart(year, month, day); } else { return false; } #endregion } /// /// 检查年月日是否合法 /// /// 年 /// 月 /// 日 /// private static bool CheckDatePart(string pYear, string pMonth, string pDay) { int iyear = Convert.ToInt16(pYear); int imonth = Convert.ToInt16(pMonth); int iday = Convert.ToInt16(pDay); if (iyear > 2099 || iyear < 1900) { return false; } if (imonth > 12 || imonth < 1) { return false; } if (iday > FuncDate.GetDaysOfMonth(iyear, imonth) || iday < 1) { return false; }; return true; } #endregion #region 正则表过式 /// /// 正则表达式验证 /// /// 验证字符 /// 正式表达式 /// 符合true不符合false public static bool CheckRegEx(string pValue, string pString) { #region Regex objAlphaPatt; objAlphaPatt = new Regex(pString, RegexOptions.Compiled); return objAlphaPatt.Match(pValue).Success; #endregion } /// /// 是否为合法的电话号码 /// /// 输入字符串 /// true/false public static bool IsTel(string pTel) { return true; } /// /// 是否为合法的邮政编码 /// /// 输入字符串 /// true/false public static bool IsZip(string pZip) { return true; } /// /// 是否为合法的电子邮件 /// /// 输入字符串 /// true/false public static bool IsEmail(string pMail) { #region Match m = RegEmail.Match(pMail); return m.Success; #endregion } /// /// 是否为合法的IP地址 /// /// /// public static bool IsIP(string pIP) { #region Match m = RegIP.Match(pIP); return m.Success; #endregion } #endregion } }