溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

驗(yàn)證數(shù)據(jù)幫助類都有哪些方法

發(fā)布時(shí)間:2020-07-09 15:25:38 來源:億速云 閱讀:121 作者:Leah 欄目:編程語言

驗(yàn)證數(shù)據(jù)幫助類都有哪些方法?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

using System;
using System.Text.RegularExpressions;

namespace ZMM.Core
{
    /// <summary>
    /// 驗(yàn)證幫助類
    /// </summary>
    public class ValidateHelper
    {
        //郵件正則表達(dá)式
        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);
        //手機(jī)號正則表達(dá)式
        private static Regex _mobileregex = new Regex("^(13|15|17|18)[0-9]{9}$");
        //固話號正則表達(dá)式
        private static Regex _phoneregex = new Regex(@"^(\d{3,4}-?)?\d{7,8}$");
        //IP正則表達(dá)式
        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])$");
        //日期正則表達(dá)式
        private static Regex _dateregex = new Regex(@"(\d{4})-(\d{1,2})-(\d{1,2})");
        //數(shù)值(包括整數(shù)和小數(shù))正則表達(dá)式
        private static Regex _numericregex = new Regex(@"^[-]?[0-9]+(\.[0-9]+)?$");
        //郵政編碼正則表達(dá)式
        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>
        /// 是否為手機(jī)號
        /// </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;//數(shù)字驗(yàn)證

            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
                return false;//省份驗(yàn)證

            string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
                return false;//生日驗(yàn)證

            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;//校驗(yàn)碼驗(yàn)證

            return true;//符合GB11643-1999標(biāo)準(zhǔn)
        }

        /// <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;//數(shù)字驗(yàn)證

            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
                return false;//省份驗(yàn)證

            string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
                return false;//生日驗(yàn)證

            return true;//符合15位身份證標(biāo)準(zhǔn)
        }

        /// <summary>
        /// 是否為日期
        /// </summary>
        public static bool IsDate(string s)
        {
            return _dateregex.IsMatch(s);
        }

        /// <summary>
        /// 是否是數(shù)值(包括整數(shù)和小數(shù))
        /// </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>
        /// 判斷一個(gè)ip是否在另一個(gè)ip內(nèi)
        /// </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>
        /// 判斷一個(gè)ip是否在另一個(gè)ip內(nèi)
        /// </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>
        /// 判斷一個(gè)ip是否在另一個(gè)ip內(nèi)
        /// </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>
        /// 判斷當(dāng)前時(shí)間是否在指定的時(shí)間段內(nèi)
        /// </summary>
        /// <param name="periodList">指定時(shí)間段</param>
        /// <param name="liePeriod">所處時(shí)間段</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>
        /// 判斷當(dāng)前時(shí)間是否在指定的時(shí)間段內(nèi)
        /// </summary>
        /// <param name="periodStr">指定時(shí)間段</param>
        /// <param name="liePeriod">所處時(shí)間段</param>
        /// <returns></returns>
        public static bool BetweenPeriod(string periodStr, out string liePeriod)
        {
            string[] periodList = StringHelper.SplitString(periodStr, "\n");
            return BetweenPeriod(periodList, out liePeriod);
        }

        /// <summary>
        /// 判斷當(dāng)前時(shí)間是否在指定的時(shí)間段內(nèi)
        /// </summary>
        /// <param name="periodList">指定時(shí)間段</param>
        /// <returns></returns>
        public static bool BetweenPeriod(string periodList)
        {
            string liePeriod = string.Empty;
            return BetweenPeriod(periodList, out liePeriod);
        }

        /// <summary>
        /// 是否是數(shù)值(包括整數(shù)和小數(shù))
        /// </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>
        /// 是否是數(shù)值(包括整數(shù)和小數(shù))
        /// </summary>
        public static bool IsNumericRule(string numericRuleStr, string splitChar)
        {
            return IsNumericArray(StringHelper.SplitString(numericRuleStr, splitChar));
        }

        /// <summary>
        /// 是否是數(shù)值(包括整數(shù)和小數(shù))
        /// </summary>
        public static bool IsNumericRule(string numericRuleStr)
        {
            return IsNumericRule(numericRuleStr, ",");
        }
    }
}

關(guān)于驗(yàn)證數(shù)據(jù)幫助類都有哪些方法問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI