专注

导航

.Net页面数据校验类(WebForm)

   1:      /// <summary>
   2:      /// 页面数据校验类
   3:      /// Copyright (C) Maticsoft 2004-2011
   4:      /// </summary>
   5:      public class PageValidate
   6:      {
   7:          private static Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$");
   8:          private static Regex RegNumber = new Regex("^[0-9]+$");
   9:          private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
  10:          private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
  11:          private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
  12:          private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 
  13:          private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
  14:   
  15:          public PageValidate()
  16:          {
  17:          }
  18:   
  19:   
  20:          #region 数字字符串检查        
  21:          public static bool IsPhone(string inputData)
  22:          {
  23:              Match m = RegPhone.Match(inputData);
  24:              return m.Success;
  25:          }
  26:          /// <summary>
  27:          /// 检查Request查询字符串的键值,是否是数字,最大长度限制
  28:          /// </summary>
  29:          /// <param name="req">Request</param>
  30:          /// <param name="inputKey">Request的键值</param>
  31:          /// <param name="maxLen">最大长度</param>
  32:          /// <returns>返回Request查询字符串</returns>
  33:          public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
  34:          {
  35:              string retVal = string.Empty;
  36:              if(inputKey != null && inputKey != string.Empty)
  37:              {
  38:                  retVal = req.QueryString[inputKey];
  39:                  if(null == retVal)
  40:                      retVal = req.Form[inputKey];
  41:                  if(null != retVal)
  42:                  {
  43:                      retVal = SqlText(retVal, maxLen);
  44:                      if(!IsNumber(retVal))
  45:                          retVal = string.Empty;
  46:                  }
  47:              }
  48:              if(retVal == null)
  49:                  retVal = string.Empty;
  50:              return retVal;
  51:          }        
  52:          /// <summary>
  53:          /// 是否数字字符串
  54:          /// </summary>
  55:          /// <param name="inputData">输入字符串</param>
  56:          /// <returns></returns>
  57:          public static bool IsNumber(string inputData)
  58:          {
  59:              Match m = RegNumber.Match(inputData);
  60:              return m.Success;
  61:          }
  62:   
  63:          /// <summary>
  64:          /// 是否数字字符串 可带正负号
  65:          /// </summary>
  66:          /// <param name="inputData">输入字符串</param>
  67:          /// <returns></returns>
  68:          public static bool IsNumberSign(string inputData)
  69:          {
  70:              Match m = RegNumberSign.Match(inputData);
  71:              return m.Success;
  72:          }        
  73:          /// <summary>
  74:          /// 是否是浮点数
  75:          /// </summary>
  76:          /// <param name="inputData">输入字符串</param>
  77:          /// <returns></returns>
  78:          public static bool IsDecimal(string inputData)
  79:          {
  80:              Match m = RegDecimal.Match(inputData);
  81:              return m.Success;
  82:          }        
  83:          /// <summary>
  84:          /// 是否是浮点数 可带正负号
  85:          /// </summary>
  86:          /// <param name="inputData">输入字符串</param>
  87:          /// <returns></returns>
  88:          public static bool IsDecimalSign(string inputData)
  89:          {
  90:              Match m = RegDecimalSign.Match(inputData);
  91:              return m.Success;
  92:          }        
  93:   
  94:          #endregion
  95:   
  96:          #region 中文检测
  97:   
  98:          /// <summary>
  99:          /// 检测是否有中文字符
 100:          /// </summary>
 101:          /// <param name="inputData"></param>
 102:          /// <returns></returns>
 103:          public static bool IsHasCHZN(string inputData)
 104:          {
 105:              Match m = RegCHZN.Match(inputData);
 106:              return m.Success;
 107:          }    
 108:   
 109:          #endregion
 110:   
 111:          #region 邮件地址
 112:          /// <summary>
 113:          /// 是否是浮点数 可带正负号
 114:          /// </summary>
 115:          /// <param name="inputData">输入字符串</param>
 116:          /// <returns></returns>
 117:          public static bool IsEmail(string inputData)
 118:          {
 119:              Match m = RegEmail.Match(inputData);
 120:              return m.Success;
 121:          }        
 122:   
 123:          #endregion
 124:   
 125:          #region 日期格式判断
 126:          /// <summary>
 127:          /// 日期格式字符串判断
 128:          /// </summary>
 129:          /// <param name="str"></param>
 130:          /// <returns></returns>
 131:          public static bool IsDateTime(string str)
 132:          {
 133:              try
 134:              {
 135:                  if (!string.IsNullOrEmpty(str))
 136:                  {
 137:                      DateTime.Parse(str);
 138:                      return true;
 139:                  }
 140:                  else
 141:                  {
 142:                      return false;
 143:                  }
 144:              }
 145:              catch
 146:              {
 147:                  return false;
 148:              }
 149:          } 
 150:          #endregion
 151:   
 152:          #region 其他
 153:   
 154:          /// <summary>
 155:          /// 检查字符串最大长度,返回指定长度的串
 156:          /// </summary>
 157:          /// <param name="sqlInput">输入字符串</param>
 158:          /// <param name="maxLength">最大长度</param>
 159:          /// <returns></returns>            
 160:          public static string SqlText(string sqlInput, int maxLength)
 161:          {            
 162:              if(sqlInput != null && sqlInput != string.Empty)
 163:              {
 164:                  sqlInput = sqlInput.Trim();                            
 165:                  if(sqlInput.Length > maxLength)//按最大长度截取字符串
 166:                      sqlInput = sqlInput.Substring(0, maxLength);
 167:              }
 168:              return sqlInput;
 169:          }        
 170:          /// <summary>
 171:          /// 字符串编码
 172:          /// </summary>
 173:          /// <param name="inputData"></param>
 174:          /// <returns></returns>
 175:          public static string HtmlEncode(string inputData)
 176:          {
 177:              return HttpUtility.HtmlEncode(inputData);
 178:          }
 179:          /// <summary>
 180:          /// 设置Label显示Encode的字符串
 181:          /// </summary>
 182:          /// <param name="lbl"></param>
 183:          /// <param name="txtInput"></param>
 184:          public static void SetLabel(Label lbl, string txtInput)
 185:          {
 186:              lbl.Text = HtmlEncode(txtInput);
 187:          }
 188:          public static void SetLabel(Label lbl, object inputObj)
 189:          {
 190:              SetLabel(lbl, inputObj.ToString());
 191:          }        
 192:          //字符串清理
 193:          public static string InputText(string inputString, int maxLength) 
 194:          {            
 195:              StringBuilder retVal = new StringBuilder();
 196:   
 197:              // 检查是否为空
 198:              if ((inputString != null) && (inputString != String.Empty)) 
 199:              {
 200:                  inputString = inputString.Trim();
 201:                  
 202:                  //检查长度
 203:                  if (inputString.Length > maxLength)
 204:                      inputString = inputString.Substring(0, maxLength);
 205:                  
 206:                  //替换危险字符
 207:                  for (int i = 0; i < inputString.Length; i++) 
 208:                  {
 209:                      switch (inputString[i]) 
 210:                      {
 211:                          case '"':
 212:                              retVal.Append("&quot;");
 213:                              break;
 214:                          case '<':
 215:                              retVal.Append("&lt;");
 216:                              break;
 217:                          case '>':
 218:                              retVal.Append("&gt;");
 219:                              break;
 220:                          default:
 221:                              retVal.Append(inputString[i]);
 222:                              break;
 223:                      }
 224:                  }                
 225:                  retVal.Replace("'", " ");// 替换单引号
 226:              }
 227:              return retVal.ToString();
 228:              
 229:          }
 230:          /// <summary>
 231:          /// 转换成 HTML code
 232:          /// </summary>
 233:          /// <param name="str">string</param>
 234:          /// <returns>string</returns>
 235:          public static string Encode(string str)
 236:          {            
 237:              str = str.Replace("&","&amp;");
 238:              str = str.Replace("'","''");
 239:              str = str.Replace("\"","&quot;");
 240:              str = str.Replace(" ","&nbsp;");
 241:              str = str.Replace("<","&lt;");
 242:              str = str.Replace(">","&gt;");
 243:              str = str.Replace("\n","<br>");
 244:              return str;
 245:          }
 246:          /// <summary>
 247:          ///解析html成 普通文本
 248:          /// </summary>
 249:          /// <param name="str">string</param>
 250:          /// <returns>string</returns>
 251:          public static string Decode(string str)
 252:          {            
 253:              str = str.Replace("<br>","\n");
 254:              str = str.Replace("&gt;",">");
 255:              str = str.Replace("&lt;","<");
 256:              str = str.Replace("&nbsp;"," ");
 257:              str = str.Replace("&quot;","\"");
 258:              return str;
 259:          }
 260:   
 261:          public static string SqlTextClear(string sqlText)
 262:          {
 263:              if (sqlText == null)
 264:              {
 265:                  return null;
 266:              }
 267:              if (sqlText == "")
 268:              {
 269:                  return "";
 270:              }
 271:              sqlText = sqlText.Replace(",", "");//去除,
 272:              sqlText = sqlText.Replace("<", "");//去除<
 273:              sqlText = sqlText.Replace(">", "");//去除>
 274:              sqlText = sqlText.Replace("--", "");//去除--
 275:              sqlText = sqlText.Replace("'", "");//去除'
 276:              sqlText = sqlText.Replace("\"", "");//去除"
 277:              sqlText = sqlText.Replace("=", "");//去除=
 278:              sqlText = sqlText.Replace("%", "");//去除%
 279:              sqlText = sqlText.Replace(" ", "");//去除空格
 280:              return sqlText;
 281:          }
 282:          #endregion
 283:   
 284:          #region 是否由特定字符组成
 285:          public static bool isContainSameChar(string strInput)
 286:          {
 287:              string charInput = string.Empty;
 288:              if (!string.IsNullOrEmpty(strInput))
 289:              {
 290:                  charInput = strInput.Substring(0, 1);
 291:              }
 292:              return isContainSameChar(strInput, charInput, strInput.Length);
 293:          }
 294:   
 295:          public static bool isContainSameChar(string strInput, string charInput, int lenInput)
 296:          {
 297:              if (string.IsNullOrEmpty(charInput))
 298:              {
 299:                  return false;
 300:              }
 301:              else
 302:              {
 303:                  Regex RegNumber = new Regex(string.Format("^([{0}])+$", charInput));
 304:                  //Regex RegNumber = new Regex(string.Format("^([{0}]{{1}})+$", charInput,lenInput));
 305:                  Match m = RegNumber.Match(strInput);
 306:                  return m.Success;
 307:              }
 308:          }
 309:          #endregion
 310:   
 311:          #region 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
 312:          /// <summary>
 313:          /// 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
 314:          /// </summary>
 315:          public static bool isContainSpecChar(string strInput)
 316:          {
 317:              string[] list = new string[] { "123456", "654321" };
 318:              bool result = new bool();
 319:              for (int i = 0; i < list.Length; i++)
 320:              {
 321:                  if (strInput == list[i])
 322:                  {
 323:                      result = true;
 324:                      break;
 325:                  }
 326:              }
 327:              return result;
 328:          }
 329:          #endregion
 330:      }

posted on 2011-11-13 12:03  陈啊M  阅读(249)  评论(0编辑  收藏  举报