您的位置:首页 > 其它

DataValidator类库源码

2016-09-13 21:01 344 查看
#region 命名空间引用

using System;

using System.Text;

using System.Text.RegularExpressions;

#endregion

/*

 *   项目名称:Pathik.DataValidator

 *     Copyright (c) 2004-2005,周  超

 *     All rights reserved.

 *    

 *     文件名称:DataValidator.cs

 *     文件标识:数据验证类

 *     摘    要:包含数据验证的多数方法,用以对字符串进行类型验证,类函数都写为静态方法,

 *               可直接使用,而无需创建类的实例

 *    

namespace Pathik.DataValidator

{

    /// <summary>

    /// ValidHelper 数据验证类。

    /// </summary>

    public class DataValidator

    {

        #region DataValidator

        /// <summary>

        /// 空构造函数

        /// </summary>

        public DataValidator()

        {

            //

            // TODO: 在此处添加构造函数逻辑

            //

        }

        #endregion

        #region IsInt32

        /// <summary>

        /// 验证字符串是否整数

        /// </summary>

        /// <param name="input">要验证的字符串</param>

        /// <returns></returns>

        public static bool IsInt32(string input)

        {

            string regexString = @"^-?\\d+$";

            return Regex.IsMatch(input, regexString);

        }

        #endregion

        #region IsDouble

        /// <summary>

        /// 验证字符串是否浮点数字

        /// </summary>

        /// <param name="v">要验证的字符串</param>

        /// <returns></returns>

        public static bool IsDouble(string input)

        {

            string regexString = @"^(-?\\d+)(\\.\\d+)?$";

            return Regex.IsMatch(input, regexString);

        }

        #endregion

        #region IsEmail

        /// <summary>

        /// 验证字符串Email地址

        /// </summary>

        /// <param name="input">要验证的字符串</param>

        /// <returns></returns>

        public static bool IsEmail(string input)

        {

            // Return true if strIn is in valid e-mail format.

            string regexString = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

            

            return Regex.IsMatch(input, regexString);

        }

        #endregion

        #region IsDate

        /// <summary>

        /// 验证字符串是否日期[2004-2-29|||2004-02-29 10:29:39 pm|||2004/12/31]

        /// </summary>

        /// <param name="v">要验证的字符串</param>

        /// <returns></returns>

        public static bool IsDate(string input)

        {

            string regexString = @"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$";

            return Regex.IsMatch(input, regexString);

            #region Description

            /*

            Expression:  ^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578]

                            )|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[4

                            69])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\

                            s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([1

                            3579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((

                            0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((

                            0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9]

                            )|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(

                            \:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$

 

            Author:  Sung Lee

            Sample Matches:  

            2004-2-29|||2004-02-29 10:29:39 pm|||2004/12/31

            Sample Non-Matches:  

            2003-2-29|||2003-13-02|||2003-2-2 10:72:30 am

            Description:  Matches ANSI SQL date format YYYY-mm-dd hh:mi:ss am/pm. You can use / - or space for date delimiters, so 2004-12-31 works just as well as 2004/12/31. Checks leap year from 1901 to 2099.

             */

            #endregion

        }

        #endregion

        #region IsAnsiSqlDate

        /// <summary>

        /// 验证字符串是否 ANSI SQL date format

        /// </summary>

        /// <param name="v">要验证的字符串</param>

        /// <returns></returns>

        public static bool IsAnsiSqlDate(string input)

        {

            string regexString = @"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578]

                                    )|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[4

                                    69])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\

                                    s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([1

                                    3579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((

                                    0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((

                                    0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9]

                                    )|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(

                                    \:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$";

            return Regex.IsMatch(input, regexString);

            #region Description

            /*

            Expression:  ^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578]

                            )|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[4

                            69])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\

                            s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([1

                            3579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((

                            0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((

                            0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9]

                            )|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(

                            \:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$

                            

                            Author:  Sung Lee

                            Sample Matches:  

                            2004-2-29|||2004-02-29 10:29:39 pm|||2004/12/31

                            Sample Non-Matches:  

                            2003-2-29|||2003-13-02|||2003-2-2 10:72:30 am

                            Description:  Matches ANSI SQL date format YYYY-mm-dd hh:mi:ss am/pm. You can use / - or space for date delimiters, so 2004-12-31 works just as well as 2004/12/31. Checks leap year from 1901 to 2099.

             */

            #endregion

        }

        #endregion

        #region IsTxtFileName

        /// <summary>

        /// 验证字符串是否TXT文件名(全名)

        /// </summary>

        /// <param name="input">要验证的字符串</param>

        /// <returns></returns>

        public static bool IsTxtFileName(string input)

        {

            string regexString = @"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w ]*))+\.(txt|TXT)$";

            return Regex.IsMatch(input, regexString);

            #region Description

            /*

            

            Expres
9ea3
sion:  ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w ]*))+\.(txt|TXT)$

 

            Author:  Michael Ash

            Sample Matches:  

            c:\file.txt|||c:\folder\sub folder\file.txt|||\\network\folder\file.txt

            Sample Non-Matches:  

            C:|||C:\file.xls|||folder.txt

            Description:  This RE validates a path/file of type txt (text file) This RE can be used as a filter on certain file types, while insuring the entire string is a fully qualified path and file. The filter value can be changed or added to as you need

             */

            #endregion

        }

        #endregion

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: