#region IsSafeSqlString 检测是否有Sql危险字符
/// <summary>
/// 检测是否有Sql危险字符
/// </summary>
/// <param name="targetString">目标字符串</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsSafeSqlString(string targetString)
{
return !Regex.IsMatch(targetString, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
}
#endregion
#region RemoveUnsafeString 过滤脚本注入和SQL注入字符
/// <summary>
/// 过滤脚本注入和SQL注入字符
/// </summary>
/// <param name="targetString">目标字符串</param>
/// <returns>过滤后的字符串</returns>
public static string RemoveUnsafeString(string targetString)
{
if (string.IsNullOrEmpty(targetString))
{
return targetString;
}
targetString = Regex.Replace(targetString, @"<script[^>]*?>.*?</script>", string.Empty, RegexOptions.IgnoreCase);
return Regex.Replace(targetString, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']", string.Empty, RegexOptions.IgnoreCase);
}
#endregion
#region IsIP 判断是否为IP
/// <summary>
/// 是否为IP
/// </summary>
/// <param name="targetString">目标字符串</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsIP(string targetString)
{
return Regex.IsMatch(targetString, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
#endregion
#region IsNumeric 判断对象是否为Int32类型的数字
/// <summary>
/// 判断对象是否为Int32类型的数字
/// </summary>
/// <param name="target">传入参数</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsNumeric(object target)
{
if (target != null)
{
return IsNumeric(target.ToString());
}
return false;
}
#endregion
#region IsNumeric 判断字符串是否为Int32类型的数字
/// <summary>
/// 判断字符串是否为Int32类型的数字
/// </summary>
/// <param name="targetString">传入参数</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsNumeric(string targetString)
{
if (targetString != null)
{
string @string = targetString;
if (@string.Length > 0 && @string.Length <= 11 && Regex.IsMatch(@string, @"^[-]?[0-9]*[.]?[0-9]*$"))
{
if ((@string.Length < 10) || (@string.Length == 10 && @string[0] == '1') || (@string.Length == 11 && @string[0] == '-' && @string[1] == '1'))
{
return true;
}
}
}
return false;
}
#endregion
#region IsPositiveInteger 验证是否为正整数
/// <summary>
/// 验证是否为正整数
/// </summary>
/// <param name="targetString">传入参数</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsPositiveInteger(string targetString)
{
return Regex.IsMatch(targetString, @"^[0-9]*$");
}
#endregion
#region IsDouble 判断对象是否为Double类型
/// <summary>
/// 是否为Double类型
/// </summary>
/// <param name="target">传入参数</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsDouble(object target)
{
if (target != null)
{
return Regex.IsMatch(target.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
}
return false;
}
#endregion
#region IsNumericArray 判断给定字符串数据中是否全为Int32类型数字
/// <summary>
/// 判断给定字符串数据中是否全为Int32类型数字
/// </summary>
/// <param name="targetArray">要确认的字符串数组</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsNumericArray(string[] targetArray)
{
if (targetArray == null)
{
return false;
}
if (targetArray.Length < 1)
{
return false;
}
return targetArray.All(IsNumeric);
}
#endregion
#region IsEmail 检测是否符合email格式
/// <summary>
/// 检测是否符合email格式
/// </summary>
/// <param name="targetString">目标字符串</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsEmail(string targetString)
{
return Regex.IsMatch(targetString, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");
}
#endregion
#region IsUrl 检测是否是有效的Url地址
/// <summary>
/// 检测是否是有效的Url地址
/// </summary>
/// <param name="targetString">目标字符串</param>
/// <returns>是则返加true 不是则返回 false</returns>
public static bool IsUrl(string targetString)
{
return Regex.IsMatch(targetString,
@"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
}
#endregion