当前位置: 移动技术网 > IT编程>开发语言>.net > 验证数据帮助类

验证数据帮助类

2018年10月18日  | 移动技术网IT编程  | 我要评论

高达之我主沉浮,混沌修真诀下载,海王金樽保质期

using system;
using system.text.regularexpressions;

namespace zmm.core
{
    /// <summary>
    /// 验证帮助类
    /// </summary>
    public class validatehelper
    {
        //邮件正则表达式
        private static regex _emailregex = new regex(@"^[a-z0-9]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$", regexoptions.ignorecase);
        //手机号正则表达式
        private static regex _mobileregex = new regex("^(13|15|17|18)[0-9]{9}$");
        //固话号正则表达式
        private static regex _phoneregex = new regex(@"^(\d{3,4}-?)?\d{7,8}$");
        //ip正则表达式
        private static regex _ipregex = new regex(@"^(\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])$");
        //日期正则表达式
        private static regex _dateregex = new regex(@"(\d{4})-(\d{1,2})-(\d{1,2})");
        //数值(包括整数和小数)正则表达式
        private static regex _numericregex = new regex(@"^[-]?[0-9]+(\.[0-9]+)?$");
        //邮政编码正则表达式
        private static regex _zipcoderegex = new regex(@"^\d{6}$");

        /// <summary>
        /// 是否为邮箱名
        /// </summary>
        public static bool isemail(string s)
        {
            if (string.isnullorempty(s))
                return true;
            return _emailregex.ismatch(s);
        }

        /// <summary>
        /// 是否为手机号
        /// </summary>
        public static bool ismobile(string s)
        {
            if (string.isnullorempty(s))
                return true;
            return _mobileregex.ismatch(s);
        }

        /// <summary>
        /// 是否为固话号
        /// </summary>
        public static bool isphone(string s)
        {
            if (string.isnullorempty(s))
                return true;
            return _phoneregex.ismatch(s);
        }

        /// <summary>
        /// 是否为ip
        /// </summary>
        public static bool isip(string s)
        {
            return _ipregex.ismatch(s);
        }

        /// <summary>
        /// 是否是身份证号
        /// </summary>
        public static bool isidcard(string id)
        {
            if (string.isnullorempty(id))
                return true;
            if (id.length == 18)
                return checkidcard18(id);
            else if (id.length == 15)
                return checkidcard15(id);
            else
                return false;
        }

        /// <summary>
        /// 是否为18位身份证号
        /// </summary>
        private static bool checkidcard18(string id)
        {
            long n = 0;
            if (long.tryparse(id.remove(17), out n) == false || n < math.pow(10, 16) || long.tryparse(id.replace('x', '0').replace('x', '0'), out n) == false)
                return false;//数字验证

            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.indexof(id.remove(2)) == -1)
                return false;//省份验证

            string birth = id.substring(6, 8).insert(6, "-").insert(4, "-");
            datetime time = new datetime();
            if (datetime.tryparse(birth, out time) == false)
                return false;//生日验证

            string[] arrvarifycode = ("1,0,x,9,8,7,6,5,4,3,2").split(',');
            string[] wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").split(',');
            char[] ai = id.remove(17).tochararray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
                sum += int.parse(wi[i]) * int.parse(ai[i].tostring());

            int y = -1;
            math.divrem(sum, 11, out y);
            if (arrvarifycode[y] != id.substring(17, 1).tolower())
                return false;//校验码验证

            return true;//符合gb11643-1999标准
        }

        /// <summary>
        /// 是否为15位身份证号
        /// </summary>
        private static bool checkidcard15(string id)
        {
            long n = 0;
            if (long.tryparse(id, out n) == false || n < math.pow(10, 14))
                return false;//数字验证

            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.indexof(id.remove(2)) == -1)
                return false;//省份验证

            string birth = id.substring(6, 6).insert(4, "-").insert(2, "-");
            datetime time = new datetime();
            if (datetime.tryparse(birth, out time) == false)
                return false;//生日验证

            return true;//符合15位身份证标准
        }

        /// <summary>
        /// 是否为日期
        /// </summary>
        public static bool isdate(string s)
        {
            return _dateregex.ismatch(s);
        }

        /// <summary>
        /// 是否是数值(包括整数和小数)
        /// </summary>
        public static bool isnumeric(string numericstr)
        {
            return _numericregex.ismatch(numericstr);
        }

        /// <summary>
        /// 是否为邮政编码
        /// </summary>
        public static bool iszipcode(string s)
        {
            if (string.isnullorempty(s))
                return true;
            return _zipcoderegex.ismatch(s);
        }

        /// <summary>
        /// 是否是图片文件名
        /// </summary>
        /// <returns> </returns>
        public static bool isimgfilename(string filename)
        {
            if (filename.indexof(".") == -1)
                return false;

            string tempfilename = filename.trim().tolower();
            string extension = tempfilename.substring(tempfilename.lastindexof("."));
            return extension == ".png" || extension == ".bmp" || extension == ".jpg" || extension == ".jpeg" || extension == ".gif";
        }
        /// <summary>
        /// 是否是文件名word
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool isfile(string filename)
        {
            if (filename.indexof(".") == -1)
                return false;

            string tempfilename = filename.trim().tolower();
            string extension = tempfilename.substring(tempfilename.lastindexof("."));
            return extension == ".doc" || extension == ".docx" || extension == ".xls" || extension == ".xlsx" || extension == ".txt";
        }

        /// <summary>
        /// 判断一个ip是否在另一个ip内
        /// </summary>
        /// <param name="sourceip">检测ip</param>
        /// <param name="targetip">匹配ip</param>
        /// <returns></returns>
        public static bool inip(string sourceip, string targetip)
        {
            if (string.isnullorempty(sourceip) || string.isnullorempty(targetip))
                return false;

            string[] sourceipblocklist = stringhelper.splitstring(sourceip, @".");
            string[] targetipblocklist = stringhelper.splitstring(targetip, @".");

            int sourceipblocklistlength = sourceipblocklist.length;

            for (int i = 0; i < sourceipblocklistlength; i++)
            {
                if (targetipblocklist[i] == "*")
                    return true;

                if (sourceipblocklist[i] != targetipblocklist[i])
                {
                    return false;
                }
                else
                {
                    if (i == 3)
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断一个ip是否在另一个ip内
        /// </summary>
        /// <param name="sourceip">检测ip</param>
        /// <param name="targetiplist">匹配ip列表</param>
        /// <returns></returns>
        public static bool iniplist(string sourceip, string[] targetiplist)
        {
            if (targetiplist != null && targetiplist.length > 0)
            {
                foreach (string targetip in targetiplist)
                {
                    if (inip(sourceip, targetip))
                        return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断一个ip是否在另一个ip内
        /// </summary>
        /// <param name="sourceip">检测ip</param>
        /// <param name="targetipstr">匹配ip</param>
        /// <returns></returns>
        public static bool iniplist(string sourceip, string targetipstr)
        {
            string[] targetiplist = stringhelper.splitstring(targetipstr, "\n");
            return iniplist(sourceip, targetiplist);
        }

        /// <summary>
        /// 判断当前时间是否在指定的时间段内
        /// </summary>
        /// <param name="periodlist">指定时间段</param>
        /// <param name="lieperiod">所处时间段</param>
        /// <returns></returns>
        public static bool betweenperiod(string[] periodlist, out string lieperiod)
        {
            if (periodlist != null && periodlist.length > 0)
            {
                datetime starttime;
                datetime endtime;
                datetime nowtime = datetime.now;
                datetime nowdate = nowtime.date;

                foreach (string period in periodlist)
                {
                    int index = period.indexof("-");
                    starttime = typehelper.stringtodatetime(period.substring(0, index));
                    endtime = typehelper.stringtodatetime(period.substring(index + 1));

                    if (starttime < endtime)
                    {
                        if (nowtime > starttime && nowtime < endtime)
                        {
                            lieperiod = period;
                            return true;
                        }
                    }
                    else
                    {
                        if ((nowtime > starttime && nowtime < nowdate.adddays(1)) || (nowtime < endtime))
                        {
                            lieperiod = period;
                            return true;
                        }
                    }
                }
            }
            lieperiod = string.empty;
            return false;
        }

        /// <summary>
        /// 判断当前时间是否在指定的时间段内
        /// </summary>
        /// <param name="periodstr">指定时间段</param>
        /// <param name="lieperiod">所处时间段</param>
        /// <returns></returns>
        public static bool betweenperiod(string periodstr, out string lieperiod)
        {
            string[] periodlist = stringhelper.splitstring(periodstr, "\n");
            return betweenperiod(periodlist, out lieperiod);
        }

        /// <summary>
        /// 判断当前时间是否在指定的时间段内
        /// </summary>
        /// <param name="periodlist">指定时间段</param>
        /// <returns></returns>
        public static bool betweenperiod(string periodlist)
        {
            string lieperiod = string.empty;
            return betweenperiod(periodlist, out lieperiod);
        }

        /// <summary>
        /// 是否是数值(包括整数和小数)
        /// </summary>
        public static bool isnumericarray(string[] numericstrlist)
        {
            if (numericstrlist != null && numericstrlist.length > 0)
            {
                foreach (string numberstr in numericstrlist)
                {
                    if (!isnumeric(numberstr))
                        return false;
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 是否是数值(包括整数和小数)
        /// </summary>
        public static bool isnumericrule(string numericrulestr, string splitchar)
        {
            return isnumericarray(stringhelper.splitstring(numericrulestr, splitchar));
        }

        /// <summary>
        /// 是否是数值(包括整数和小数)
        /// </summary>
        public static bool isnumericrule(string numericrulestr)
        {
            return isnumericrule(numericrulestr, ",");
        }
    }
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网