验证工具类

public class Regexlib
{
/// <summary>
/// 判断字符串是否是a-zA-Z0-9_范围内(4,18位范围内)
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidUserName(string strIn)
{
return Regex.IsMatch(strIn, @"^[A-Za-z0-9_]{4,18}$");
}

/// <summary>
/// 以汉字或字母开头 、汉字与字母数字组合的名称,长度为4到16位
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidNickName(string strIn)
{
return Regex.IsMatch(strIn, @"^[\u4E00-\u9FA5A-Za-z0-9]{4,18}$");
}

/// <summary>
/// 判断真实姓名
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidRealName(string strIn)
{
return Regex.IsMatch(strIn, @"[\u4e00-\u9fa5a-zA-Z]{2,20}");
}

/// <summary>
/// 验证邮箱
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidEmail(string strIn)
{
return Regex.IsMatch(strIn, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
}
/// <summary>
/// 验证手机
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidMobile(string strIn)
{
return Regex.IsMatch(strIn, @"^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$");
}

/// <summary>
/// Author:Geek Dog
/// Content:验证手机号
/// Time:2016-10-31 13:52:44
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidTelPhone(string strIn)
{
return Regex.IsMatch(strIn, @"^0?(1)[0-9]{10}$");
}

/// <summary>
/// Author:Geek Dog
/// Content: 验证用户密码(a-zA-Z0-9_范围内6~18位)
/// Time:2016-10-31 13:52:14
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidUserPwd(string strIn)
{
return Regex.IsMatch(strIn, @"^(?!([^A-Za-z]|\d)+$)[a-zA-Z\d]{6,16}$");
}

/// <summary>
/// Author:Geek Dog
/// Content:验证GUID (数字字母组合)
/// Time:2016-10-31 13:55:27
/// </summary>
/// <returns></returns>
public static bool IsValidGuId(string strIn)
{
return Regex.IsMatch(strIn, @"^[A-Za-z0-9]{10,50}$");
}

/// <summary>
/// Author:Geek Dog
/// Content:验证纯数字(最低个位,最高千位)
/// Time:2016-10-31 14:06:58
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidNum(string strIn)
{
return Regex.IsMatch(strIn, @"^[0-9]{1,8}$");
}

/// <summary>
/// Author:Geek Dog
/// Content:验证纯数字(最低个位,最高千位)
/// Time:2016-10-31 14:06:58
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidBankNum(string strIn)
{
return Regex.IsMatch(strIn, @"^[0-9]{16,19}$");
}

/// <summary>
/// Author:Geek Dog
/// Content:验证URL地址
/// Time:2016-10-31 13:53:02
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidUrlAddress(string strIn)
{
string strWhere = @"^(http|https|ftp)\\://([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])|([a-zA-Z0"
+ "-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.[a-zA-Z]{2,4})(\\:[0-9]+)?(/"
+ "[^/][a-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\$\\=~_\\-@]*)*$";
return Regex.IsMatch(strIn, strWhere);
}

/// <summary>
/// 检验验证码是否合法
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsValCode(string str)
{
return Regex.IsMatch(str, @"^[0-9a-zA-Z]*$");
}

/// <summary>
/// 检验手机号码是否合法
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsValMobileCode(string str)
{
return Regex.IsMatch(str, @"^[0-9]*$");
}

/// <summary>
/// 判断输入的是否为小数
/// </summary>
/// <param name="decimals"></param>
/// <returns></returns>
public static bool IsDecimals(string decimals)
{
return Regex.IsMatch(decimals, @"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$");
}

public static bool IsQQ(string qq)
{
return Regex.IsMatch(qq, @"^([1-9])\d{4,13}$");
}

/// <summary>
/// 输入框内容验证
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static bool GetIsFormText(string text)
{
#region 防止XSS攻击
if (text.IndexOf("<") != -1) {
return false;
}

if (text.IndexOf(">") != -1) {
return false;
}
#endregion

#region 防止sql注入式攻击
string StrKeyWord = @"select|insert|delete|from|count\(|drop table|update|truncate|asc\(|mid\(|char\(|xp_cmdshell|exec master|netlocalgroup administrators|:|net user|""|or|and";
//过滤关键字符
string StrRegex = @"[-|;|,|/|\(|\)|\[|\]|}|{|%|\@|*|!|']";
if (Regex.IsMatch(text, StrKeyWord, RegexOptions.IgnoreCase) || Regex.IsMatch(text, StrRegex)) {
return false;
}
#endregion
return true;
}

}

posted @ 2017-08-21 20:09  !opts  阅读(134)  评论(0编辑  收藏  举报