zoukankan      html  css  js  c++  java
  • 用于验证的公共类

    /** 1. 功能:操作正则表达式的公共类  
     
    **/ 
    #region 引用命名空间   
    using System;   
    using System.Collections.Generic;   
    using System.Text;   
    using System.Text.RegularExpressions;  
    #endregion   
      
      
    namespace Comm   
    {   
        /// <summary>   
        /// 操作正则表达式的公共类   
        /// </summary>       
        public class RegexHelper   
        {  
            #region 验证输入的字符串是否合法   
            /// <summary>   
            /// 验证输入的字符串是否合法,合法返回true,否则返回false。   
            /// </summary>   
            /// <param name="strInput">输入的字符串</param>   
            /// <param name="strPattern">模式字符串</param>           
            public static bool Validate( string strInput , string strPattern )   
            {   
                return Regex.IsMatch( strInput , strPattern );   
            }  
            #endregion   
        }   
    }  
     
    View Code    
      
    /** 1. 功能:用于验证的公共类  
     
    **/ 
     
    #region 引用命名空间   
    using System;   
    using System.Collections.Generic;   
    using System.Text;   
    using System.Text.RegularExpressions;  
    #endregion   
      
    namespace Comm   
    {   
        /// <summary>   
        /// 用于验证的公共类   
        /// </summary>   
        public class ValidationHelper   
        {  
            #region 判断对象是否为空   
            /// <summary>   
            /// 判断对象是否为空,为空返回true   
            /// </summary>   
            /// <typeparam name="T">要验证的对象的类型</typeparam>   
            /// <param name="data">要验证的对象</param>           
            public static bool IsNullOrEmpty<T>( T data )   
            {   
                //如果为null   
                if ( data == null )   
                {   
                    return true;   
                }   
      
                //如果为""   
                if ( data.GetType() == typeof( String ) )   
                {   
                    if ( string.IsNullOrEmpty( data.ToString().Trim() ) )   
                    {   
                        return true;   
                    }   
                }   
      
                //如果为DBNull   
                if ( data.GetType() == typeof( DBNull ) )   
                {   
                    return true;   
                }   
      
                //不为空   
                return false;   
            }   
      
            /// <summary>   
            /// 判断对象是否为空,为空返回true   
            /// </summary>   
            /// <param name="data">要验证的对象</param>   
            public static bool IsNullOrEmpty( object data )   
            {   
                //如果为null   
                if ( data == null )   
                {   
                    return true;   
                }   
      
                //如果为""   
                if ( data.GetType() == typeof( String ) )   
                {   
                    if ( string.IsNullOrEmpty( data.ToString().Trim() ) )   
                    {   
                        return true;   
                    }   
                }   
      
                //如果为DBNull   
                if ( data.GetType() == typeof( DBNull ) )   
                {   
                    return true;   
                }   
      
                //不为空   
                return false;   
            }  
            #endregion  
     
            #region 验证IP地址是否合法   
            /// <summary>   
            /// 验证IP地址是否合法   
            /// </summary>   
            /// <param name="ip">要验证的IP地址</param>           
            public static bool IsIP( string ip )   
            {   
                //如果为空,认为验证合格   
                if ( IsNullOrEmpty( ip ) )   
                {   
                    return true;   
                }   
      
                //清除要验证字符串中的空格   
                ip = ip.Trim();   
      
                //模式字符串   
                string pattern = @"^((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)$";   
      
                //验证   
                return RegexHelper.Validate( ip, pattern );   
            }  
            #endregion  
     
            #region  验证EMail是否合法   
            /// <summary>   
            /// 验证EMail是否合法   
            /// </summary>   
            /// <param name="email">要验证的Email</param>   
            public static bool IsEmail( string email )   
            {   
                //如果为空,认为验证合格   
                if ( IsNullOrEmpty( email ) )   
                {   
                    return true;   
                }   
      
                //清除要验证字符串中的空格   
                email = email.Trim();   
      
                //模式字符串   
                string pattern = @"^([0-9a-zA-Z]([-.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+[a-zA-Z]{2,9})$";   
      
                //验证   
                return RegexHelper.Validate( email, pattern );   
            }  
            #endregion  
     
            #region 验证是否为整数   
            /// <summary>   
            /// 验证是否为整数   
            /// </summary>   
            /// <param name="number">要验证的整数</param>           
            public static bool IsInt( string number )   
            {   
                //如果为空,认为验证合格   
                if ( IsNullOrEmpty( number ) )   
                {   
                    return true;   
                }   
      
                //清除要验证字符串中的空格   
                number = number.Trim();   
      
                //模式字符串   
                string pattern = @"^[1-9]+[0-9]*$";   
      
                //验证   
                return RegexHelper.Validate( number, pattern );   
            }  
            #endregion  
     
            #region 验证是否为数字   
            /// <summary>   
            /// 验证是否为数字   
            /// </summary>   
            /// <param name="number">要验证的数字</param>           
            public static bool IsNumber( string number )   
            {   
                //如果为空,认为验证合格   
                if ( IsNullOrEmpty( number ) )   
                {   
                    return true;   
                }   
      
                //清除要验证字符串中的空格   
                number = number.Trim();   
      
                //模式字符串   
                string pattern = @"^[1-9]+[0-9]*[.]?[0-9]*$";   
      
                //验证   
                return RegexHelper.Validate( number, pattern );   
            }  
            #endregion  
     
            #region 验证日期是否合法   
            /// <summary>   
            /// 验证日期是否合法,对不规则的作了简单处理   
            /// </summary>   
            /// <param name="date">日期</param>   
            public static bool IsDate( ref string date )   
            {   
                //如果为空,认为验证合格   
                if ( IsNullOrEmpty( date ) )   
                {   
                    return true;   
                }   
      
                //清除要验证字符串中的空格   
                date = date.Trim();   
      
                //替换   
                date = date.Replace( @"","-" );   
                //替换/   
                date = date.Replace( @"/", "-" );   
      
                //如果查找到汉字"今",则认为是当前日期   
                if ( date.IndexOf( "今" ) != -1 )   
                {   
                    date = DateTime.Now.ToString();   
                }   
      
                try  
                {   
                    //用转换测试是否为规则的日期字符   
                    date = Convert.ToDateTime( date ).ToString( "d" );   
                    return true;   
                }   
                catch  
                {   
                    //如果日期字符串中存在非数字,则返回false   
                    if ( !IsInt( date ) )   
                    {   
                        return false;   
                    }  
     
                    #region 对纯数字进行解析   
                    //对8位纯数字进行解析   
                    if ( date.Length == 8 )   
                    {   
                        //获取年月日   
                        string year = date.Substring( 0, 4 );   
                        string month = date.Substring( 4, 2 );   
                        string day = date.Substring( 6, 2 );   
      
                        //验证合法性   
                        if ( Convert.ToInt32( year ) < 1900 || Convert.ToInt32( year ) > 2100 )   
                        {   
                            return false;   
                        }   
                        if ( Convert.ToInt32( month ) > 12 || Convert.ToInt32( day ) > 31 )   
                        {   
                            return false;   
                        }   
      
                        //拼接日期   
                        date = Convert.ToDateTime( year + "-" + month + "-" + day ).ToString( "d" );   
                        return true;   
                    }   
      
                    //对6位纯数字进行解析   
                    if ( date.Length == 6 )   
                    {   
                        //获取年月   
                        string year = date.Substring( 0, 4 );   
                        string month = date.Substring( 4, 2 );   
      
                        //验证合法性   
                        if ( Convert.ToInt32( year ) < 1900 || Convert.ToInt32( year ) > 2100 )   
                        {   
                            return false;   
                        }   
                        if ( Convert.ToInt32( month ) > 12 )   
                        {   
                            return false;   
                        }   
      
                        //拼接日期   
                        date = Convert.ToDateTime( year + "-" + month ).ToString( "d" );   
                        return true;   
                    }   
      
                    //对5位纯数字进行解析   
                    if ( date.Length == 5 )   
                    {   
                        //获取年月   
                        string year = date.Substring( 0, 4 );   
                        string month = date.Substring( 4, 1 );   
      
                        //验证合法性   
                        if ( Convert.ToInt32( year ) < 1900 || Convert.ToInt32( year ) > 2100 )   
                        {   
                            return false;   
                        }   
      
                        //拼接日期   
                        date = year + "-" + month;   
                        return true;   
                    }   
      
                    //对4位纯数字进行解析   
                    if ( date.Length == 4 )   
                    {   
                        //获取年   
                        string year = date.Substring( 0, 4 );   
      
                        //验证合法性   
                        if ( Convert.ToInt32( year ) < 1900 || Convert.ToInt32( year ) > 2100 )   
                        {   
                            return false;   
                        }   
      
                        //拼接日期   
                        date = Convert.ToDateTime( year ).ToString( "d" );   
                        return true;   
                    }  
                    #endregion   
      
                    return false;   
                }   
            }  
            #endregion  
     
            #region 验证身份证是否合法   
            /// <summary>   
            /// 验证身份证是否合法   
            /// </summary>   
            /// <param name="idCard">要验证的身份证</param>           
            public static bool IsIdCard( string idCard )   
            {   
                //如果为空,认为验证合格   
                if ( IsNullOrEmpty( idCard ) )   
                {   
                    return true;   
                }   
      
                //清除要验证字符串中的空格   
                idCard = idCard.Trim();   
      
                //模式字符串   
                StringBuilder pattern = new StringBuilder();   
                pattern.Append( @"^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|" );   
                pattern.Append( @"50|51|52|53|54|61|62|63|64|65|71|81|82|91)" );   
                pattern.Append( @"(d{13}|d{15}[dx])$" );   
      
                //验证   
                return RegexHelper.Validate( idCard, pattern.ToString() );   
            }  
            #endregion  
     
            #region 检测客户的输入中是否有危险字符串   
            /// <summary>   
            /// 检测客户输入的字符串是否有效,并将原始字符串修改为有效字符串或空字符串。   
            /// 当检测到客户的输入中有攻击性危险字符串,则返回false,有效返回true。   
            /// </summary>   
            /// <param name="input">要检测的字符串</param>   
            public static bool IsValidInput( ref string input )   
            {   
                try  
                {   
                    if ( IsNullOrEmpty( input ) )   
                    {   
                        //如果是空值,则跳出   
                        return true;   
                    }   
                    else  
                    {   
                        //替换单引号   
                        input = input.Replace( "''", "''''" ).Trim();   
      
                        //检测攻击性危险字符串   
                        string testString = "and |or |exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";   
                        string[] testArray = testString.Split( ''|'' );   
                        foreach ( string testStr in testArray )   
                        {   
                            if ( input.ToLower().IndexOf( testStr ) != -1 )   
                            {   
                                //检测到攻击字符串,清空传入的值   
                                input = "";   
                                return false;   
                            }   
                        }   
      
                        //未检测到攻击字符串   
                        return true;   
                    }   
                }   
                catch ( Exception ex )   
                {   
                    LogHelper.WriteTraceLog( TraceLogLevel.Error, ex.Message );   
                    return false;   
                }   
            }  
            #endregion   
        }   
    }  

    佛为心,道为骨,儒为表,大度看世界; 技在手,能在身,思在脑,从容过生活; 三千年读史,不外功名利禄; 九万里悟道,终归诗酒田园;
  • 相关阅读:
    持续集成-禅道
    nohup.out 日志切分
    Flannel 介绍及使用场景
    【Unity游戏开发】初探Unity动画优化
    fastHttp服务端处理请求的过程
    PHPExcel导出文件代码实现
    PHPExcel 1.8
    CKfinder 安装与使用
    Ckeditor的配置
    Ckeditor的使用
  • 原文地址:https://www.cnblogs.com/taofx/p/4136947.html
Copyright © 2011-2022 走看看