using System;
using System.Text.RegularExpressions;
namespace System.Common
{
public class ValidateString
{
#region 字符串验证
#region 判断字符串是否为整型
/**/
///
/// 判断字符串是否为整型
///
///
///
#endregion
public static bool IsInteger(string source)
{
if (string.IsNullOrEmpty(source))
{
return false;
}
if (Regex.IsMatch(source, "^((\\+)\\d)?\\d*$"))
{
return true;
}
return false;
}
public static bool IsZInteger(string source)
{
if (string.IsNullOrEmpty(source))
{
return false;
}
if (Regex.IsMatch(source, "^[1-9][0-9]*$"))
{
return true;
}
return false;
}
#region 判断字符串是否为整型
/**/
///
/// 判断字符串是否为整型
///
///
///
#endregion
public static bool IsDecimal(string source)
{
return IsMoney(source);
}
#region Email 格式是否合法
/**/
///
/// Email 格式是否合法
///
///
#endregion
public static bool IsEmail(string strEmail)
{
return Regex.IsMatch(strEmail, @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
}
#region 判断一个字符串是否为网址
/**/
///
/// 判断一个字符串是否为网址
///
///
///
#endregion
public static bool IsUrl(string _value)
{
var regex = new Regex(@"(http://)?([\w-]+\.)*[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.IgnoreCase);
return regex.Match(_value).Success;
}
#region 判断是否IP
/**/
///
/// 判断是否IP
///
///
///
#endregion
public static bool IsIP(string source)
{
return Regex.IsMatch(source, @"^(((25[0-5]|2[0-4][0-9]|19[0-1]|19[3-9]|18[0-9]|17[0-1]|17[3-9]|1[0-6][0-9]|1[1-9]|[2-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))|(192\.(25[0-5]|2[0-4][0-9]|16[0-7]|169|1[0-5][0-9]|1[7-9][0-9]|[1-9][0-9]|[0-9]))|(172\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|1[0-5]|3[2-9]|[4-9][0-9]|[0-9])))\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$");
}
#region 检查字符串是否为A-Z、0-9及下划线以内的字符
/**/
///
/// 检查字符串是否为A-Z、0-9及下划线以内的字符
///
/// 被检查的字符串
/// 是否有特殊字符
#endregion
public static bool IsLetterOrNumber(string str)
{
bool b = Regex.IsMatch(str, "[a-zA-Z0-9_]");
return b;
}
#region 检查字符串是否0-9以内的字符
/**/
///
/// 检查字符串是否为0-9以内的字符
///
/// 被检查的字符串
/// 是否有特殊字符
#endregion
public static bool IsNumberStr(string str)
{
bool b = Regex.IsMatch(str, "^[0-9]*$");
return b;
}
#region 返回字符串某个位置的一个字符的类型
/**/
///
/// 返回字符串某个位置的几个字符是否为a-zA-Z的字母
///
/// 被检查的字符串
/// 字符位置
/// 长度
/// 是否是字母
#endregion
public static bool GetStringType(string str, int Site, int num)
{
string a = str.Substring(Site, num);
bool b = Regex.IsMatch(a, "[a-zA-Z]");
return b;
}
#region 验输入字符串是否含有“/\<>:.?*|$]”特殊字符
/**/
///
/// 验输入字符串是否含有“/\:.?*|$]”特殊字符
///
///
///
#endregion
public static bool IsSpecialChar(string source)
{
var r = new Regex(@"[/\<>:.?*|$]");
return r.IsMatch(source);
}
#region 是否全为中文/日文/韩文字符
/**/
///
/// 是否全为中文/日文/韩文字符
///
/// 源字符串
///
#endregion
public static bool IsChineseChar(string source)
{
//中文/日文/韩文: [\u4E00-\u9FA5]
//英文:[a-zA-Z]
return Regex.IsMatch(source, @"^[\u4E00-\u9FA5]+$");
}
#region 是否包含双字节字符(允许有单字节字符)
/**/
///
/// 是否包含双字节字符(允许有单字节字符)
///
///
///
#endregion
public static bool IsDoubleChar(string source)
{
return Regex.IsMatch(source, @"[^\x00-\xff]");
}
#region 是否为日期型字符串
/**/
///
/// 是否为日期型字符串
///
/// 日期字符串(2005-6-30)
///
#endregion
public static bool IsDate(string source)
{
return Regex.IsMatch(source, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
}
#region 是否为时间型字符串
/**/
///
/// 是否为时间型字符串
///
/// 时间字符串(15:00:00)
///
#endregion
public static bool IsTime(string source)
{
return Regex.IsMatch(source, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
}
#region 是否为日期+时间型字符串
/**/
///
/// 是否为日期+时间型字符串
///
///
///
#endregion
public static bool IsDateTime(string source)
{
return Regex.IsMatch(source, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) ((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
}
#region 是否为钱币型数据
/**/
///
/// 是否为钱币型数据
///
///
///
#endregion
public static bool IsMoney(string source)
{
if (source == "")
{
return false;
}
return Regex.IsMatch(source, @"^(([1-9]\d*)|0)(\.\d{1,2})?$");
}
#region 判断是否为空字符串
/**/
///
/// 是否为空字符串
///
///
///
#endregion
public static bool IsNull(object obj)
{
bool rn = obj == null || Convert.ToString(obj).Length <= 0 || Convert.IsDBNull(obj);
return rn;
}
#endregion
}
}