常用字符验证与过滤函数

来源:互联网 发布:2016如何做好淘宝网店 编辑:程序博客网 时间:2024/06/07 06:03

作者: chixiaojin 用户权限:版主 用户积分: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]+$"); 
      }
      
      

   }
原创粉丝点击