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
}
}
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
}
}
相关文章推荐
- 关于C++类库KYLib: 使用AVLtree实现 map 类模板源码
- 【第三方类库】underscore.js源码---each forEach 每次迭代跟{}比较的疑惑
- 利用源码编译Android系统Java类库
- 策略模式解析-JAVA类库中TreeSet源码为例
- asp.Net_图片上传的一个类库的源码
- PHP 汉字转拼音源码(GB2312类库,大约支持6000个汉字左右)
- 非常出色的一款WinForm窗体重绘GUI类库源码
- 从源码编译Android系统的Java类库和JNI动态库的方法
- Android studio导入第三方类库源码以及jar包
- Android studio如何导入第三方类库源码
- 介绍一款非常出色的WinForm窗体重绘GUI类库源码
- jdk类库源码分析-各个包
- .NET 类库源码
- 实用的开放源码的Excel导入导出类库 CarlosAg ExcelXmlWriter
- IDAPython类库---idc.py的源码
- OpenCV中常用类库含义和源码
- vendor类库导入 分析源码 (thinkphp3.1)
- Java 源码学习线路————_先JDK工具包集合_再core包,也就是String、StringBuffer等_Java IO类库
- 玄机论坛Socket类库源码 当前版本 2.6.3 更新日期:10-09/2015 z
- 几个通用组件类库项目 开放源码下载