欧美日韩一区二区三区四区不卡,日韩欧美视频一区二区三区四区,久久精品欧美一区二区三区不卡,国产精品久久久乱弄

咨詢電話:186 7916 6165 咨詢電話:186 7916 6165 (微信同號)    在線QQ:181796286
NEWS BLOG ·
學無止境
關注開優網絡 關注前沿
ASP.NET公共類庫之驗證碼類ValidateImage.cs
ASP.NET公共類庫之XML處理基類XMLHelper.cs

ASP.NET公共類庫之驗證實用類ValidateUtils.cs

發表日期:2015-09-07    文章編輯:南昌開優網絡    瀏覽次數:3952    標簽:ASP.NET應用

/**********************************************
 * 類作用:   驗證實用類
 * 作者:開優網絡
 * http://www.568387.com
 ***********************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Svnhost.Common
{
    public class ValidateUtils
    {
        #region 數據類型驗證


        /// <summary>
        /// 判斷是否為base64字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsBase64String(string str)
        {
            //A-Z, a-z, 0-9, +, /, =
            return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
        }

        /// <summary>
        /// 驗證是否為正整數
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsInt(string str)
        {
            return Regex.IsMatch(str, @"^[0-9]*$");
        }

        /// <summary>
        /// 判斷對象是否為Int32類型的數字
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(object Expression)
        {
            if (Expression != null)
            {
                string str = Expression.ToString();
                if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
                {
                    if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                    {
                        return true;
                    }
                }
            }
            return false;

        }


        public static bool IsDouble(object Expression)
        {
            if (Expression != null)
            {
                return Regex.IsMatch(Expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
            }
            return false;
        }



        /// <summary>
        /// 判斷給定的字符串數組(strNumber)中的數據是不是都為數值型
        /// </summary>
        /// <param name="strNumber">要確認的字符串數組</param>
        /// <returns>是則返加true 不是則返回 false</returns>
        public static bool IsNumericArray(string[] strNumber)
        {
            if (strNumber == null)
            {
                return false;
            }
            if (strNumber.Length < 1)
            {
                return false;
            }
            foreach (string id in strNumber)
            {
                if (!IsNumeric(id))
                {
                    return false;
                }
            }
            return true;

        }

        #endregion


        #region 字符串功能驗證

        /// <summary>
        /// 是否包涵制定的字符串
        /// </summary>
        /// <param name="str"></param>
        /// <param name="stringarray"></param>
        /// <param name="strsplit"></param>
        /// <returns></returns>
        public static bool IsCompriseStr(string str, string stringarray, string strsplit)
        {
            if (stringarray == "" || stringarray == null)
            {
                return false;
            }

            str = str.ToLower();
            string[] stringArray = StringUtil.SplitString(stringarray.ToLower(), strsplit);
            for (int i = 0; i < stringArray.Length; i++)
            {
                if (str.IndexOf(stringArray[i]) > -1)
                {
                    return true;
                }
            }
            return false;
        }
        
        /// <summary>
        /// 判斷指定字符串是否屬于指定字符串數組中的一個元素
        /// </summary>
        /// <param name="strSearch">字符串</param>
        /// <param name="stringArray">字符串數組</param>
        /// <param name="caseInsensetive">是否不區分大小寫, true為不區分, false為區分</param>
        /// <returns>判斷結果</returns>
        public static bool InArray(string strSearch, string[] stringArray, bool caseInsensetive)
        {
            return StringUtil.GetInArrayID(strSearch, stringArray, caseInsensetive) >= 0;
        }

        /// <summary>
        /// 判斷指定字符串是否屬于指定字符串數組中的一個元素
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="stringarray">字符串數組</param>
        /// <returns>判斷結果</returns>
        public static bool InArray(string str, string[] stringarray)
        {
            return InArray(str, stringarray, false);
        }

        /// <summary>
        /// 判斷指定字符串是否屬于指定字符串數組中的一個元素
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="stringarray">內部以逗號分割單詞的字符串</param>
        /// <returns>判斷結果</returns>
        public static bool InArray(string str, string stringarray)
        {
            return InArray(str, StringUtil.SplitString(stringarray, ","), false);
        }

        /// <summary>
        /// 判斷指定字符串是否屬于指定字符串數組中的一個元素
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="stringarray">內部以逗號分割單詞的字符串</param>
        /// <param name="strsplit">分割字符串</param>
        /// <returns>判斷結果</returns>
        public static bool InArray(string str, string stringarray, string strsplit)
        {
            return InArray(str, StringUtil.SplitString(stringarray, strsplit), false);
        }

        /// <summary>
        /// 判斷指定字符串是否屬于指定字符串數組中的一個元素
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="stringarray">內部以逗號分割單詞的字符串</param>
        /// <param name="strsplit">分割字符串</param>
        /// <param name="caseInsensetive">是否不區分大小寫, true為不區分, false為區分</param>
        /// <returns>判斷結果</returns>
        public static bool InArray(string str, string stringarray, string strsplit, bool caseInsensetive)
        {
            return InArray(str, StringUtil.SplitString(stringarray, strsplit), caseInsensetive);
        }



        /// <summary>是否空</summary>
        /// <param name="strInput">輸入字符串</param>
        /// <returns>true/false</returns>
        public static bool isBlank(string strInput)
        {
            if (strInput == null || strInput.Trim() == "")
            {
                return true;
            }
            else
            {
                return false;
            }

        }


        #endregion

        #region IP地址 SQL注入等安全驗證


        /// <summary>
        /// 是否為ip
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");

        }


        public static bool IsIPSect(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){2}((2[0-4]\d|25[0-5]|[01]?\d\d?|\*)\.)(2[0-4]\d|25[0-5]|[01]?\d\d?|\*)$");

        }



        /// <summary>
        /// 檢測是否有Sql危險字符
        /// </summary>
        /// <param name="str">要判斷字符串</param>
        /// <returns>判斷結果</returns>
        public static bool IsSafeSqlString(string str)
        {
            return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
        }

        /// <summary>
        /// 檢測是否有危險的可能用于鏈接的字符串
        /// </summary>
        /// <param name="str">要判斷字符串</param>
        /// <returns>判斷結果</returns>
        public static bool IsSafeUserInfoString(string str)
        {
            return !Regex.IsMatch(str, @"^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|游客|^Guest");
        }

        /// <summary>
        /// 返回指定IP是否在指定的IP數組所限定的范圍內, IP數組內的IP地址可以使用*表示該IP段任意, 例如192.168.1.*
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="iparray"></param>
        /// <returns></returns>
        public static bool InIPArray(string ip, string[] iparray)
        {

            string[] userip = StringUtil.SplitString(ip, @".");
            for (int ipIndex = 0; ipIndex < iparray.Length; ipIndex++)
            {
                string[] tmpip = StringUtil.SplitString(iparray[ipIndex], @".");
                int r = 0;
                for (int i = 0; i < tmpip.Length; i++)
                {
                    if (tmpip[i] == "*")
                    {
                        return true;
                    }

                    if (userip.Length > i)
                    {
                        if (tmpip[i] == userip[i])
                        {
                            r++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }

                }
                if (r == 4)
                {
                    return true;
                }


            }
            return false;

        }

        #endregion


        #region 其他功能驗證

        /// <summary>
        /// 判斷文件流是否為UTF8字符集
        /// </summary>
        /// <param name="sbInputStream">文件流</param>
        /// <returns>判斷結果</returns>
        private static bool IsUTF8(FileStream sbInputStream)
        {
            int i;
            byte cOctets;  // octets to go in this UTF-8 encoded character 
            byte chr;
            bool bAllAscii = true;
            long iLen = sbInputStream.Length;

            cOctets = 0;
            for (i = 0; i < iLen; i++)
            {
                chr = (byte)sbInputStream.ReadByte();

                if ((chr & 0x80) != 0) bAllAscii = false;

                if (cOctets == 0)
                {
                    if (chr >= 0x80)
                    {
                        do
                        {
                            chr <<= 1;
                            cOctets++;
                        }
                        while ((chr & 0x80) != 0);

                        cOctets--;
                        if (cOctets == 0) return false;
                    }
                }
                else
                {
                    if ((chr & 0xC0) != 0x80)
                    {
                        return false;
                    }
                    cOctets--;
                }
            }

            if (cOctets > 0)
            {
                return false;
            }

            if (bAllAscii)
            {
                return false;
            }

            return true;

        }       
 

        #endregion


        #region 日期類型格式驗證
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool IsTime(string timeval)
        {
            return Regex.IsMatch(timeval, @"^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
        }

        /// <summary>
        /// 判斷字符串是否是yy-mm-dd字符串
        /// </summary>
        /// <param name="str">待判斷字符串</param>
        /// <returns>判斷結果</returns>
        public static bool IsDateString(string str)
        {
            return Regex.IsMatch(str, @"(\d{4})-(\d{1,2})-(\d{1,2})");
        }

        /// <summary>是否日期</summary>
        /// <param name="strInput">輸入字符串</param>
        /// <returns>true/false</returns>
        public static bool isDate(string strInput)
        {
            string datestr = strInput;
            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;
            }

        }

        /// <summary>
        /// 檢查年月日是否合法
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="part"></param>
        /// <returns></returns>
        private static bool checkDatePart(string year, string month, string day)
        {
            int iyear = Convert.ToInt16(year);
            int imonth = Convert.ToInt16(month);
            int iday = Convert.ToInt16(day);
            if (iyear > 2099 || iyear < 1900) { return false; }
            if (imonth > 12 || imonth < 1) { return false; }
            if (iday > DateUtil.GetDaysOfMonth(iyear, imonth) || iday < 1) { return false; };
            return true;
        }

        #endregion
  
    }
}
主站蜘蛛池模板: 繁昌县| 大方县| 玛纳斯县| 高阳县| 霍邱县| 叶城县| 阜城县| 新沂市| 德安县| 兴山县| 华宁县| 凤山县| 孟连| 屯留县| 南皮县| 河东区| 晋宁县| 芦山县| 平昌县| 梅州市| 鄂伦春自治旗| 黎川县| 调兵山市| 高唐县| 湟中县| 肇东市| 曲水县| 津南区| 临漳县| 阜城县| 广灵县| 玛纳斯县| 敖汉旗| 光山县| 田林县| 双柏县| 大竹县| 三江| 乌鲁木齐市| 武安市| 鄯善县|