.NET后台验证
2015-06-12 15:55
671 查看
// <summary>
/// 提供经常需要使用的一些验证逻辑。 比如 邮箱是否合法
/// </summary>
public class Validator
{
/// <summary>
/// 检查一个字符串是否可以转化为日期,一般用于验证用户输入日期的合法性。
/// </summary>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否可以转化为日期的bool值。</returns>
public static bool IsStringDate(string _value)
{
DateTime dTime;
try
{
dTime = DateTime.Parse(_value);
}
catch (FormatException e)
{
//日期格式不正确时
Console.WriteLine(e.Message);
return false;
}
return true;
}
/// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
Regex RegCHZN = new Regex("[/u4e00-/u9fa5]");
Match m = RegCHZN.Match(inputData);
return m.Success;
}
/// <summary>
/// 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。
/// </summary>
/// <param name="_value">需验证的字符串。。</param>
/// <returns>是否合法的bool值。</returns>
public static bool IsNumeric(string _value)
{
return Validator.QuickValidate("^[1-9]*[0-9]*$", _value);
}
/// <summary>
/// 检查一个字符串是否是纯字母和数字构成的,一般用于查询字符串参数的有效性验证。
/// </summary>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否合法的bool值。</returns>
public static bool IsLetterOrNumber(string _value)
{
return Validator.QuickValidate("^[a-zA-Z0-9_]*$", _value);
}
/// <summary>
/// 判断是否是数字,包括小数和整数。
/// </summary>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否合法的bool值。</returns>
public static bool IsNumber(string _value)
{
return Validator.QuickValidate("^(0|([1-9]+[0-9]*))(.[0-9]+)?$", _value);
}
/// <summary>
/// 快速验证一个字符串是否符合指定的正则表达式。
/// </summary>
/// <param name="_express">正则表达式的内容。</param>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否合法的bool值。</returns>
public static bool QuickValidate(string _express, string _value)
{
System.Text.RegularExpressions.Regex myRegex = new System.Text.RegularExpressions.Regex(_express);
if (_value.Length == 0)
{
return false;
}
return myRegex.IsMatch(_value);
}
/// <summary>
/// 判断一个字符串是否为邮件
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsEmail(string _value)
{
Regex regex = new Regex(@"^/w+([-+.]/w+)*@(/w+([-.]/w+)*/.)+([a-zA-Z]+)+$", RegexOptions.IgnoreCase);
return regex.Match(_value).Success;
}
/// <summary>
/// 判断一个字符串是否为ID格式
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsIDCard(string _value)
{
Regex regex;
string[] strArray;
DateTime time;
if ((_value.Length != 15) && (_value.Length != 0x12))
{
return false;
}
if (_value.Length == 15)
{
regex = new Regex(@"^(/d
)(/d
)(/d
)(/d
)(/d
)$");
if (!regex.Match(_value).Success)
{
return false;
}
strArray = regex.Split(_value);
try
{
time = new DateTime(int.Parse("19" + strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4]));
return true;
}
catch
{
return false;
}
}
regex = new Regex(@"^(/d
)(/d
)(/d
)(/d
)(/d
)([0-9Xx])$");
if (!regex.Match(_value).Success)
{
return false;
}
strArray = regex.Split(_value);
try
{
time = new DateTime(int.Parse(strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4]));
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 判断一个字符串是否为Int
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsInt(string _value)
{
Regex regex = new Regex(@"^(-){0,1}/d+$");
if (regex.Match(_value).Success)
{
if ((long.Parse(_value) > 0x7fffffffL) || (long.Parse(_value) < -2147483648L))
{
return false;
}
return true;
}
return false;
}
/// <summary>
/// 判断一个字符串是否为手机号码
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsMobileNum(string _value)
{
Regex regex = new Regex(@"^13/d
$", RegexOptions.IgnoreCase);
return regex.Match(_value).Success;
}
/// <summary>
/// 判断一个字符串是否为电话号码
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsPhoneNum(string _value)
{
Regex regex = new Regex(@"^(86)?(-)?(0/d{2,3})?(-)?(/d{7,8})(-)?(/d{3,5})?$", RegexOptions.IgnoreCase);
return regex.Match(_value).Success;
}
/// <summary>
/// 判断一个字符串是否为网址
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsUrl(string _value)
{
Regex regex = new Regex(@"(http://)?([/w-]+/.)*[/w-]+(/[/w- ./?%&=]*)?", RegexOptions.IgnoreCase);
return regex.Match(_value).Success;
}
/// <summary>
/// 判断一个字符串是否为字母加数字
/// Regex("[a-zA-Z0-9]?"
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsWordAndNum(string _value)
{
Regex regex = new Regex("[a-zA-Z0-9]?");
return regex.Match(_value).Success;
}
/// <summary>
/// 把字符串转成日期
/// </summary>
/// <param name="_value">字符串</param>
/// <param name="_defaultValue">默认值</param>
/// <returns></returns>
public static DateTime StrToDate(string _value, DateTime _defaultValue)
{
if (IsStringDate(_value))
return Convert.ToDateTime(_value);
else
return _defaultValue;
}
/// <summary>
/// 把字符串转成整型
/// </summary>
/// <param name="_value">字符串</param>
/// <param name="_defaultValue">默认值</param>
/// <returns></returns>
public static int StrToInt(string _value, int _defaultValue)
{
if (IsNumber(_value))
return int.Parse(_value);
else
return _defaultValue;
}
}
/// 提供经常需要使用的一些验证逻辑。 比如 邮箱是否合法
/// </summary>
public class Validator
{
/// <summary>
/// 检查一个字符串是否可以转化为日期,一般用于验证用户输入日期的合法性。
/// </summary>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否可以转化为日期的bool值。</returns>
public static bool IsStringDate(string _value)
{
DateTime dTime;
try
{
dTime = DateTime.Parse(_value);
}
catch (FormatException e)
{
//日期格式不正确时
Console.WriteLine(e.Message);
return false;
}
return true;
}
/// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
Regex RegCHZN = new Regex("[/u4e00-/u9fa5]");
Match m = RegCHZN.Match(inputData);
return m.Success;
}
/// <summary>
/// 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。
/// </summary>
/// <param name="_value">需验证的字符串。。</param>
/// <returns>是否合法的bool值。</returns>
public static bool IsNumeric(string _value)
{
return Validator.QuickValidate("^[1-9]*[0-9]*$", _value);
}
/// <summary>
/// 检查一个字符串是否是纯字母和数字构成的,一般用于查询字符串参数的有效性验证。
/// </summary>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否合法的bool值。</returns>
public static bool IsLetterOrNumber(string _value)
{
return Validator.QuickValidate("^[a-zA-Z0-9_]*$", _value);
}
/// <summary>
/// 判断是否是数字,包括小数和整数。
/// </summary>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否合法的bool值。</returns>
public static bool IsNumber(string _value)
{
return Validator.QuickValidate("^(0|([1-9]+[0-9]*))(.[0-9]+)?$", _value);
}
/// <summary>
/// 快速验证一个字符串是否符合指定的正则表达式。
/// </summary>
/// <param name="_express">正则表达式的内容。</param>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否合法的bool值。</returns>
public static bool QuickValidate(string _express, string _value)
{
System.Text.RegularExpressions.Regex myRegex = new System.Text.RegularExpressions.Regex(_express);
if (_value.Length == 0)
{
return false;
}
return myRegex.IsMatch(_value);
}
/// <summary>
/// 判断一个字符串是否为邮件
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsEmail(string _value)
{
Regex regex = new Regex(@"^/w+([-+.]/w+)*@(/w+([-.]/w+)*/.)+([a-zA-Z]+)+$", RegexOptions.IgnoreCase);
return regex.Match(_value).Success;
}
/// <summary>
/// 判断一个字符串是否为ID格式
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsIDCard(string _value)
{
Regex regex;
string[] strArray;
DateTime time;
if ((_value.Length != 15) && (_value.Length != 0x12))
{
return false;
}
if (_value.Length == 15)
{
regex = new Regex(@"^(/d
)(/d
)(/d
)(/d
)(/d
)$");
if (!regex.Match(_value).Success)
{
return false;
}
strArray = regex.Split(_value);
try
{
time = new DateTime(int.Parse("19" + strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4]));
return true;
}
catch
{
return false;
}
}
regex = new Regex(@"^(/d
)(/d
)(/d
)(/d
)(/d
)([0-9Xx])$");
if (!regex.Match(_value).Success)
{
return false;
}
strArray = regex.Split(_value);
try
{
time = new DateTime(int.Parse(strArray[2]), int.Parse(strArray[3]), int.Parse(strArray[4]));
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 判断一个字符串是否为Int
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsInt(string _value)
{
Regex regex = new Regex(@"^(-){0,1}/d+$");
if (regex.Match(_value).Success)
{
if ((long.Parse(_value) > 0x7fffffffL) || (long.Parse(_value) < -2147483648L))
{
return false;
}
return true;
}
return false;
}
/// <summary>
/// 判断一个字符串是否为手机号码
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsMobileNum(string _value)
{
Regex regex = new Regex(@"^13/d
$", RegexOptions.IgnoreCase);
return regex.Match(_value).Success;
}
/// <summary>
/// 判断一个字符串是否为电话号码
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsPhoneNum(string _value)
{
Regex regex = new Regex(@"^(86)?(-)?(0/d{2,3})?(-)?(/d{7,8})(-)?(/d{3,5})?$", RegexOptions.IgnoreCase);
return regex.Match(_value).Success;
}
/// <summary>
/// 判断一个字符串是否为网址
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsUrl(string _value)
{
Regex regex = new Regex(@"(http://)?([/w-]+/.)*[/w-]+(/[/w- ./?%&=]*)?", RegexOptions.IgnoreCase);
return regex.Match(_value).Success;
}
/// <summary>
/// 判断一个字符串是否为字母加数字
/// Regex("[a-zA-Z0-9]?"
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
public static bool IsWordAndNum(string _value)
{
Regex regex = new Regex("[a-zA-Z0-9]?");
return regex.Match(_value).Success;
}
/// <summary>
/// 把字符串转成日期
/// </summary>
/// <param name="_value">字符串</param>
/// <param name="_defaultValue">默认值</param>
/// <returns></returns>
public static DateTime StrToDate(string _value, DateTime _defaultValue)
{
if (IsStringDate(_value))
return Convert.ToDateTime(_value);
else
return _defaultValue;
}
/// <summary>
/// 把字符串转成整型
/// </summary>
/// <param name="_value">字符串</param>
/// <param name="_defaultValue">默认值</param>
/// <returns></returns>
public static int StrToInt(string _value, int _defaultValue)
{
if (IsNumber(_value))
return int.Parse(_value);
else
return _defaultValue;
}
}
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 星外ASP.Net的安全设置相关说明
- C#、ASP.NET通用扩展工具类之TypeParse
- C#、ASP.NET通用扩展工具类之LogicSugar
- C#、ASP.NET通用工具类IsWhat?(可以判断数字、身份证、数据类型等等)
- 程序中常用的种代码
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- ASP.NET页面间的传值的几种方法
- 入侵ASP.net网站的经验附利用代码
- asp.net OleDbCommand 的用法
- jquery调用asp.net 页面后台的实现代码
- asp.net中的check与uncheck关键字用法解析
- ASP.NET MVC 4 捆绑和缩小实例介绍
- ASP.Net Post方式获取数据流的一种简单写法
- asp.net动态产生checkbox(数据源为DB或内存集合)
- Asp.Net 和 AJAX.Net 的区别第1/2页
- asp.net利用后台实现直接生成html分页的方法
- asp.net用三层实现多条件检索示例
- ASP.NET Cookie 操作实现
- asp.net SqlDataAdapter对象使用札记