常用字符验证与过滤函数
来源:互联网 发布:2016如何做好淘宝网店 编辑:程序博客网 时间:2024/06/07 06:03
作者: 版主 用户积分:232
public class Validation
{
#region 构造函数
public Validation()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion
private static string str;
/// <summary>
/// 常用过滤字符
///防止注入攻击
/// </summary>
public static string IsValidSting(string strIn)
{
str = strIn.Replace("'","<@>1000");
str = str.Replace(",","<@>1001");
str = str.Replace("/"","<@>1002");
str = str.Replace("UPDATE","<@>1003");
str = str.Replace("update","<@>1004");
str = str.Replace("DELETE","<@>1005");
str = str.Replace("delete","<@>1006");
str = str.Replace("SELECT","<@>1007");
str = str.Replace("select","<@>1008");
return str;
}
/// <summary>
/// 常用还原字符
/// </summary>
public static string IsValidToSting(string strIn)
{
str = strIn.Replace("<@>1000","'");
str = str.Replace("<@>1001",",");
str = str.Replace("<@>1002","/"");
str = str.Replace("<@>1003","UPDATE");
str = str.Replace("<@>1004","update");
str = str.Replace("<@>1005","DELETE");
str = str.Replace("<@>1006","delete");
str = str.Replace("<@>1007","SELECT");
str = str.Replace("<@>1008","select");
return str;
}
/// <summary>
/// 有效密码检验
/// </summary>
public static bool IsValidPassword(string strIn)
{
if (strIn.Length<6 || strIn.Length>20)
{
return false;
}
else
{
return Regex.IsMatch(strIn, "^[A-Za-z0-9]+$");
}
}
/// <summary>
/// 有效电子邮件格式检验
/// </summary>
public static bool IsValidEmail(string strIn)
{
return Regex.IsMatch(strIn, @"^([/w-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$");
}
/// <summary>
/// 有效URL检验
/// </summary>
public static bool IsValidUrl(string strIn)
{
return Regex.IsMatch(strIn, @"^http://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?$");
}
/// <summary>
/// 有效日期检验 如2005-9-2
/// 基本上把闰年和2月等的情况都考虑进去了
/// </summary>
public static bool IsValidDate(string strIn)
{
return Regex.IsMatch(strIn, @"^((((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-))$");
}
/// <summary>
/// 有效长日期检验 如2005-9-2 13:20:37
/// 基本上把闰年和2月等的情况都考虑进去了
/// </summary>
public static bool IsValidDateLong(string strIn)
{
return Regex.IsMatch(strIn, @"^((((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$");
}
/// <summary>
/// 有效身份证检验
/// </summary>
public static bool IsValidIDCard(string strIn)
{
return Regex.IsMatch(strIn,@"^(/d{18}|/d{15})+$");
}
/// <summary>
/// 有效邮政编码检验
/// </summary>
public static bool IsValidPcode(string strIn)
{
return Regex.IsMatch(strIn,@"^(/d{6})+$");
}
/// <summary>
/// 有效电话检验
/// 如0599-1234567-0000 或 0599-1234567 等
/// </summary>
public static bool IsValidTel(string strIn)
{
return Regex.IsMatch(strIn,@"^(((/d{3}|/d{4})-(/d{7}|/d{8}))|((/d{3}|/d{4})-(/d{7}|/d{8})-(/d{4})))$");
}
/// <summary>
/// 有效整数检验
/// </summary>
public static bool IsValidInt(string strIn)
{
//[0-9]也可
return Regex.IsMatch(strIn, "^(//d)+$");
}
/// <summary>
/// 有效英文检验
/// </summary>
public static bool IsValidEnglish(string strIn)
{
return Regex.IsMatch(strIn, "^([A-Za-z])+$");
}
/// <summary>
/// 有效中文检验
/// </summary>
public static bool IsValidChinese(string strIn)
{
return Regex.IsMatch(strIn, "^([/u4e00-/u9fa5])+$");
}
/// <summary>
/// 有效英文-数字检验
/// </summary>
public static bool IsValidEnglishInt(string strIn)
{
//[A-Za-z|0-9]也可以
return Regex.IsMatch(strIn, "^[A-Za-z0-9]+$");
}
/// <summary>
/// 有效中文-数字检验
/// </summary>
public static bool IsValidChineseInt(string strIn)
{
return Regex.IsMatch(strIn, "^[0-9/u4e00-/u9fa5]+$");
}
/// <summary>
/// 有效中文-英文-数字检验
/// </summary>
public static bool IsValidChineseEnglishInt(string strIn)
{
return Regex.IsMatch(strIn, "^[A-Za-z0-9/u4e00-/u9fa5]+$");
}
}
用户权限:public class Validation
{
#region 构造函数
public Validation()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion
private static string str;
/// <summary>
/// 常用过滤字符
///防止注入攻击
/// </summary>
public static string IsValidSting(string strIn)
{
str = strIn.Replace("'","<@>1000");
str = str.Replace(",","<@>1001");
str = str.Replace("/"","<@>1002");
str = str.Replace("UPDATE","<@>1003");
str = str.Replace("update","<@>1004");
str = str.Replace("DELETE","<@>1005");
str = str.Replace("delete","<@>1006");
str = str.Replace("SELECT","<@>1007");
str = str.Replace("select","<@>1008");
return str;
}
/// <summary>
/// 常用还原字符
/// </summary>
public static string IsValidToSting(string strIn)
{
str = strIn.Replace("<@>1000","'");
str = str.Replace("<@>1001",",");
str = str.Replace("<@>1002","/"");
str = str.Replace("<@>1003","UPDATE");
str = str.Replace("<@>1004","update");
str = str.Replace("<@>1005","DELETE");
str = str.Replace("<@>1006","delete");
str = str.Replace("<@>1007","SELECT");
str = str.Replace("<@>1008","select");
return str;
}
/// <summary>
/// 有效密码检验
/// </summary>
public static bool IsValidPassword(string strIn)
{
if (strIn.Length<6 || strIn.Length>20)
{
return false;
}
else
{
return Regex.IsMatch(strIn, "^[A-Za-z0-9]+$");
}
}
/// <summary>
/// 有效电子邮件格式检验
/// </summary>
public static bool IsValidEmail(string strIn)
{
return Regex.IsMatch(strIn, @"^([/w-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$");
}
/// <summary>
/// 有效URL检验
/// </summary>
public static bool IsValidUrl(string strIn)
{
return Regex.IsMatch(strIn, @"^http://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?$");
}
/// <summary>
/// 有效日期检验 如2005-9-2
/// 基本上把闰年和2月等的情况都考虑进去了
/// </summary>
public static bool IsValidDate(string strIn)
{
return Regex.IsMatch(strIn, @"^((((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-))$");
}
/// <summary>
/// 有效长日期检验 如2005-9-2 13:20:37
/// 基本上把闰年和2月等的情况都考虑进去了
/// </summary>
public static bool IsValidDateLong(string strIn)
{
return Regex.IsMatch(strIn, @"^((((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$");
}
/// <summary>
/// 有效身份证检验
/// </summary>
public static bool IsValidIDCard(string strIn)
{
return Regex.IsMatch(strIn,@"^(/d{18}|/d{15})+$");
}
/// <summary>
/// 有效邮政编码检验
/// </summary>
public static bool IsValidPcode(string strIn)
{
return Regex.IsMatch(strIn,@"^(/d{6})+$");
}
/// <summary>
/// 有效电话检验
/// 如0599-1234567-0000 或 0599-1234567 等
/// </summary>
public static bool IsValidTel(string strIn)
{
return Regex.IsMatch(strIn,@"^(((/d{3}|/d{4})-(/d{7}|/d{8}))|((/d{3}|/d{4})-(/d{7}|/d{8})-(/d{4})))$");
}
/// <summary>
/// 有效整数检验
/// </summary>
public static bool IsValidInt(string strIn)
{
//[0-9]也可
return Regex.IsMatch(strIn, "^(//d)+$");
}
/// <summary>
/// 有效英文检验
/// </summary>
public static bool IsValidEnglish(string strIn)
{
return Regex.IsMatch(strIn, "^([A-Za-z])+$");
}
/// <summary>
/// 有效中文检验
/// </summary>
public static bool IsValidChinese(string strIn)
{
return Regex.IsMatch(strIn, "^([/u4e00-/u9fa5])+$");
}
/// <summary>
/// 有效英文-数字检验
/// </summary>
public static bool IsValidEnglishInt(string strIn)
{
//[A-Za-z|0-9]也可以
return Regex.IsMatch(strIn, "^[A-Za-z0-9]+$");
}
/// <summary>
/// 有效中文-数字检验
/// </summary>
public static bool IsValidChineseInt(string strIn)
{
return Regex.IsMatch(strIn, "^[0-9/u4e00-/u9fa5]+$");
}
/// <summary>
/// 有效中文-英文-数字检验
/// </summary>
public static bool IsValidChineseEnglishInt(string strIn)
{
return Regex.IsMatch(strIn, "^[A-Za-z0-9/u4e00-/u9fa5]+$");
}
}
- 常用字符验证与过滤函数
- 两个常用的危险字符过滤函数
- 字符在验证 过滤
- 字符过滤函数
- 过滤非法字符函数
- 字符过滤函数
- 过滤特殊字符函数
- 过滤、替换、转义、验证函数
- asp 过滤非法字符函数
- php 过滤特殊字符函数
- jquery验证文本框,以及特殊字符过滤
- 字符/单词提取与过滤
- 常用正则表达式(通用字符过滤)
- PHP中强大的过滤验证函数
- php中filter函数用来验证、过滤
- php中filter函数用来验证、过滤
- Asp过滤某些特殊字符的函数
- 我收藏的特殊字符过滤函数
- 用ASP.NET上传大文件
- 产品经理的第一堂课(四):质量还是质量
- Java5 多线程实践
- delphi 2006-05笔记
- 忙碌的一天
- 常用字符验证与过滤函数
- 打印全排列
- 最近积累的一些小东西
- 菜鸟都可以拥有属于自己的代理服务器!
- 程序员的十大谎言
- Tomcat下JSP、Servlet和JavaBean环境的配置
- 不能将项目“DataControl”中的依赖项“Database, Version=1.0.1021.11237, Culture=neutral”复制到运行目录,因为它将改写引用“Database, Version=1.0.2211.29717, Cul
- 分配 ,指针,c++
- 中学时代的回忆