DataValidator类库源码

#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
            /*
            
            Expression:  ^(([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
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值