string检测类

Posted on 2007-08-17 15:50  东倒西歪  阅读(258)  评论(1)    收藏  举报

平时对字符串的检测很头痛,今天整理了一下,收集了一些常用的检测方法,写了如下的一个类,该类只做参考,有什么不同见解还请多多指教呀。。
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace EE.Common.Validate
{
    /// <summary>
    /// 页面数据校验类
    /// xxx
    /// 2007.7
    /// </summary>
    public class PageValidate
    {
        public enum RegexType
        {

            /// <summary>
            /// 整数
            /// </summary>
            rtInt = 0,
            /// <summary>
            /// 正整数
            /// </summary>
            rtPInt = 1,
            /// <summary>
            /// 负整数
            /// </summary>
            rtMInt = 2,
            /// <summary>
            /// 数字
            /// </summary>
            rtNum = 3,
            /// <summary>
            /// 正数
            /// </summary>
            rtPNum = 4,
            /// <summary>
            /// 负数
            /// </summary>
            rtMNum = 5,
            /// <summary>
            /// 浮点数
            /// </summary>
            rtFloat = 6,
            /// <summary>
            /// 正浮点数
            /// </summary>
            rtPFloat = 7,
            /// <summary>
            /// 负浮点数
            /// </summary>
            rtMFloat = 8,
            /// <summary>
            /// 邮件
            /// </summary>
            rtEmail = 9,
            /// <summary>
            /// 颜色
            /// </summary>
            rtColor = 10,
            /// <summary>
            /// url联接
            /// </summary>
            rtUrl = 11,
            /// <summary>
            /// 仅中文
            /// </summary>
            rtChinese = 12,
            /// <summary>
            /// 仅ACSII字符
            /// </summary>
            rtAscii = 13,
            /// <summary>
            /// 邮编
            /// </summary>
            rtZipcode = 14,
            /// <summary>
            /// 手机
            /// </summary>
            rtMobile = 15,
            /// <summary>
            /// ip地址
            /// </summary>
            rtIP = 16,
            /// <summary>
            /// 非空
            /// </summary>
            rtNotempty = 17,
            /// <summary>
            /// 图片
            /// </summary>
            rtPicture = 18,
            /// <summary>
            /// 压缩文件
            /// </summary>
            rtRar = 19,
            /// <summary>
            /// 日期
            /// </summary>
            rtDate = 20,
            /// <summary>
            /// 时间
            /// </summary>
            rtTime = 21
        }

        public PageValidate()
        {

        }
        /// <summary>
        /// 字符串检测
        /// </summary>
        /// <param name="rt"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool StrCheck(RegexType rt, string str)
        {

            string rgStr = string.Empty;
            switch (rt)
            {
                case RegexType.rtInt:
                    rgStr = "^([+-]?)\\d+$";
                    break;
                case RegexType.rtPInt:
                    rgStr = "^([+]?)\\d+$";
                    break;
                case RegexType.rtMInt:
                    rgStr = "^-\\d+$";
                    break;
                case RegexType.rtNum:
                    rgStr = "^([+-]?)\\d*\\.?\\d+$";
                    break;
                case RegexType.rtPNum:
                    rgStr = "^([+]?)\\d*\\.?\\d+$";
                    break;
                case RegexType.rtMNum:
                    rgStr = "^-\\d*\\.?\\d+$";
                    break;
                case RegexType.rtFloat:
                    rgStr = "^([+-]?)\\d*\\.\\d+$";
                    break;
                case RegexType.rtPFloat:
                    rgStr = "^([+]?)\\d*\\.\\d+$";
                    break;
                case RegexType.rtMFloat:
                    rgStr = "^-\\d*\\.\\d+$";
                    break;
                case RegexType.rtEmail:
                    rgStr = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\.\\w+([-.]\\w+)*";
                    break;
                case RegexType.rtColor:
                    rgStr = "^#[a-fA-F0-9]{6}";
                    break;
                case RegexType.rtUrl:
                    rgStr = "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$";
                    break;
                case RegexType.rtChinese:
                    rgStr = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";
                    break;
                case RegexType.rtAscii:
                    rgStr = "^[\\x00-\\xFF]+$";
                    break;
                case RegexType.rtZipcode:
                    rgStr = "^\\d{6}$";
                    break;
                case RegexType.rtMobile:
                    rgStr = "^0{0,1}13[0-9]{9}$";
                    break;
                case RegexType.rtIP:
                    rgStr = "^\\(([0-1]\\d{0,2})|(2[0-5]{0,2}))\\.(([0-1]\\d{0,2})|(2[0-5]{0,2}))\\.(([0-1]\\d{0,2})|(2[0-5]{0,2}))\\.(([0-1]\\d{0,2})|(2[0-5]{0,2}))$";
                    break;
                case RegexType.rtNotempty:
                    rgStr = "^\\S+$";
                    break;
                case RegexType.rtPicture:
                    rgStr = "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$";
                    break;
                case RegexType.rtRar:
                    rgStr = "(.*)\\.(rar|zip|7zip|tgz)$";
                    break;
                case RegexType.rtDate:
                    rgStr = "^\\d{2,4}[\\/\\-]?((((0?[13578])|(1[02]))[\\/|\\-]?((0?[1-9]|[0-2][0-9])|(3[01])))|(((0?[469])|(11))[\\/|\\-]?((0?[1-9]|[0-2][0-9])|(30)))|(0?[2][\\/\\-]?(0?[1-9]|[0-2][0-9])))$";
                    break;
                case RegexType.rtTime:
                    rgStr = "^(20|21|22|23|[01]\\d|\\d)(([:.][0-5]\\d){1,2})$";
                    break;
            }

            Regex rg = new Regex(rgStr);
            Match m = rg.Match(str);
            return m.Success;
        }

        /// <summary>
        /// 是否为整数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IntStr(string str)
        {
            return StrCheck(RegexType.rtInt, str);
        }

        /// <summary>
        /// 是否为正整数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool PIntStr(string str)
        {
            return StrCheck(RegexType.rtPInt, str);
        }
        /// <summary>
        /// 是否负整数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool MIntStr(string str)
        {
            return StrCheck(RegexType.rtMInt, str);
        }

        /// <summary>
        /// 是否为数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool NumStr(string str)
        {
            return StrCheck(RegexType.rtNum, str);
        }
        /// <summary>
        /// 是否为正数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool PNumStr(string str)
        {
            return StrCheck(RegexType.rtPNum, str);
        }
        /// <summary>
        /// 是否为负数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool MNumStr(string str)
        {
            return StrCheck(RegexType.rtMNum, str);
        }
        /// <summary>
        /// 是否为浮点数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool FloatStr(string str)
        {
            return StrCheck(RegexType.rtFloat, str);
        }
        /// <summary>
        /// 是否为正浮点数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool PFloatStr(string str)
        {
            return StrCheck(RegexType.rtPFloat, str);
        }
        /// <summary>
        /// 是否为负浮点数
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool MFloatStr(string str)
        {
            return StrCheck(RegexType.rtMFloat, str);
        }

        //------------------
        /// <summary>
        /// 邮箱
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool EmailStr(string str)
        {
            return StrCheck(RegexType.rtEmail, str);
        }
        /// <summary>
        /// 颜色
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ColorStr(string str)
        {
            return StrCheck(RegexType.rtColor, str);
        }

        /// <summary>
        /// url链接地址
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool UrlStr(string str)
        {
            return StrCheck(RegexType.rtUrl, str);
        }
        /// <summary>
        /// 仅为汉字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ChinesStr(string str)
        {
            return StrCheck(RegexType.rtChinese, str);
        }
        /// <summary>
        /// 仅为Ascii码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool AsciiStr(string str)
        {
            return StrCheck(RegexType.rtAscii, str);
        }
        /// <summary>
        /// 邮编
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ZipcodeStr(string str)
        {
            return StrCheck(RegexType.rtZipcode, str);
        }

        //----------
        /// <summary>
        /// 手机
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool MobileStr(string str)
        {
            return StrCheck(RegexType.rtMobile, str);
        }
        /// <summary>
        /// IP地址
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IPStr(string str)
        {
            return StrCheck(RegexType.rtIP, str);
        }
        /// <summary>
        /// 非空
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool NotemptyStr(string str)
        {
            return StrCheck(RegexType.rtNotempty, str);
        }
        /// <summary>
        /// 是否为图片
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool PictureStr(string str)
        {
            return StrCheck(RegexType.rtPicture, str);
        }
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool RarStr(string str)
        {
            return StrCheck(RegexType.rtRar, str);
        }
        /// <summary>
        /// 日期
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool DateStr(string str)
        {
            return StrCheck(RegexType.rtDate, str);
        }

        /// <summary>
        /// 时间
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool TimeStr(string str)
        {
            return StrCheck(RegexType.rtTime, str);
        }

        /// <summary>
        /// 字符串匹配检测
        /// </summary>
        /// <param name="checkStr">匹配字符串</param>
        /// <param name="regStr">检测字符串</param>
        /// <returns></returns>
        public static bool RegexCheck(string checkStr, string regStr)
        {

            Regex rg = new Regex(regStr);
            Match mc = rg.Match(checkStr);
            return mc.Success;
        }
    }
}