#region IsGuid(是否Guid) /// <summary> /// 是否Guid /// </summary> /// <param>Guid字符串</param> /// <returns></returns> public static bool IsGuid(string guid) { if (guid.IsEmpty()) { return false; } return guid.IsMatch(@"[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}|[A-F0-9]{32}", RegexOptions.IgnoreCase); } #endregion #region IsPositiveInteger(是否大于0的正整数) /// <summary> /// 是否大于0的正整数 /// </summary> /// <param>正整数</param> /// <returns></returns> public static bool IsPositiveInteger(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^[1-9]+\d*$"); } #endregion #region IsInt32(是否Int32类型) /// <summary> /// 是否Int32类型 /// </summary> /// <param>整数</param> /// <returns></returns> public static bool IsInt32(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^[0-9]*$"); } #endregion #region IsDouble(是否Double类型,如果带有.默认为1位0) /// <summary> /// 是否Double类型 /// </summary> /// <param>小数</param> /// <returns></returns> public static bool IsDouble(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^\d[.]?\d?$"); } /// <summary> /// 是否Double类型 /// </summary> /// <param>小数</param> /// <param>最小值</param> /// <param>最大值</param> /// <param>小数位数,如果是0则不检测</param> /// <returns></returns> public static bool IsDouble(string value, double minValue, double maxValue, int digit) { if (value.IsEmpty()) { return false; } string patten = string.Format(@"^\d[.]?\d{0}$", "{0,10}"); if (digit > 0) { patten = string.Format(@"^\d[.]?\d{0}$", "{" + digit + "}"); } if (value.IsMatch(patten)) { double val = Convert.ToDouble(value); if (val >= minValue && val <= maxValue) { return true; } } return false; } #endregion #region IsInteger(是否整数) /// <summary> /// 是否整数 /// </summary> /// <param>值</param> /// <returns>结果</returns> public static bool IsInteger(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^\-?[0-9]+$"); } #endregion #region IsUnicode(是否Unicode字符串) /// <summary> /// 是否Unicode字符串 /// </summary> /// <param>unicode字符串</param> /// <returns>结果</returns> public static bool IsUnicode(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch( @"^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$"); } #endregion #region IsDecimal(是否数字型) /// <summary> /// 是否数字型 /// </summary> /// <param>数字</param> /// <returns></returns> public static bool IsDecimal(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^([0-9])[0-9]*(\.\w*)?$"); } #endregion
2.8 IP、Mac地址验证
#region IsMac(是否Mac地址) /// <summary> /// 是否Mac地址 /// </summary> /// <param>Mac地址</param> /// <returns></returns> public static bool IsMac(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^([0-9A-F]{2}-){5}[0-9A-F]{2}$") || value.IsMatch(@"^[0-9A-F]{12}$"); } #endregion #region IsIpAddress(是否IP地址) /// <summary> /// 是否IP地址 /// </summary> /// <param>ip地址</param> /// <returns>结果</returns> public static bool IsIpAddress(string value) { if (value.IsEmpty()) { return false; } return value.IsMatch(@"^(\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d\.){3}\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d$"); }
2.9 字符串验证