C#-手写页面数据校验类
1 using System; 2 using System.Globalization; 3 using System.Text; 4 using System.Text.RegularExpressions; 5 using System.Web; 6 using System.Web.UI.WebControls; 7 8 namespace Util 9 { 10 /// <summary> 11 /// 页面数据校验类 12 /// Copyright (C) MES 2004-2012 13 /// </summary> 14 public class PageValidate 15 { 16 private static Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$"); 17 private static Regex RegNumber = new Regex("^[0-9]+$"); 18 private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$"); 19 private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$"); 20 private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$ 21 private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 22 private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); 23 24 public PageValidate() 25 { 26 } 27 28 #region 数字字符串检查 29 public static bool IsPhone(string inputData) 30 { 31 Match m = RegPhone.Match(inputData); 32 return m.Success; 33 } 34 /// <summary> 35 /// 检查Request查询字符串的键值,是否是数字,最大长度限制 36 /// </summary> 37 /// <param name="req">Request</param> 38 /// <param name="inputKey">Request的键值</param> 39 /// <param name="maxLen">最大长度</param> 40 /// <returns>返回Request查询字符串</returns> 41 public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen) 42 { 43 string retVal = string.Empty; 44 if(inputKey != null && inputKey != string.Empty) 45 { 46 retVal = req.QueryString[inputKey]; 47 if(null == retVal) 48 retVal = req.Form[inputKey]; 49 if(null != retVal) 50 { 51 retVal = SqlText(retVal, maxLen); 52 if(!IsNumber(retVal)) 53 retVal = string.Empty; 54 } 55 } 56 if(retVal == null) 57 retVal = string.Empty; 58 return retVal; 59 } 60 /// <summary> 61 /// 是否数字字符串 62 /// </summary> 63 /// <param name="inputData">输入字符串</param> 64 /// <returns></returns> 65 public static bool IsNumber(string inputData) 66 { 67 Match m = RegNumber.Match(inputData); 68 return m.Success; 69 } 70 71 /// <summary> 72 /// 是否数字字符串 可带正负号 73 /// </summary> 74 /// <param name="inputData">输入字符串</param> 75 /// <returns></returns> 76 public static bool IsNumberSign(string inputData) 77 { 78 Match m = RegNumberSign.Match(inputData); 79 return m.Success; 80 } 81 /// <summary> 82 /// 是否是浮点数 83 /// </summary> 84 /// <param name="inputData">输入字符串</param> 85 /// <returns></returns> 86 public static bool IsDecimal(string inputData) 87 { 88 Match m = RegDecimal.Match(inputData); 89 return m.Success; 90 } 91 /// <summary> 92 /// 是否是浮点数 可带正负号 93 /// </summary> 94 /// <param name="inputData">输入字符串</param> 95 /// <returns></returns> 96 public static bool IsDecimalSign(string inputData) 97 { 98 Match m = RegDecimalSign.Match(inputData); 99 return m.Success; 100 } 101 102 #endregion 103 104 #region 中文检测 105 106 /// <summary> 107 /// 检测是否有中文字符 108 /// </summary> 109 /// <param name="inputData"></param> 110 /// <returns></returns> 111 public static bool IsHasCHZN(string inputData) 112 { 113 Match m = RegCHZN.Match(inputData); 114 return m.Success; 115 } 116 117 #endregion 118 119 #region 邮件地址 120 /// <summary> 121 /// 是否是浮点数 可带正负号 122 /// </summary> 123 /// <param name="inputData">输入字符串</param> 124 /// <returns></returns> 125 public static bool IsEmail(string inputData) 126 { 127 Match m = RegEmail.Match(inputData); 128 return m.Success; 129 } 130 131 #endregion 132 133 #region 日期格式判断 134 /// <summary> 135 /// 日期格式字符串判断 136 /// </summary> 137 /// <param name="str"></param> 138 /// <returns></returns> 139 public static bool IsDateTime(string str) 140 { 141 try 142 { 143 if (!string.IsNullOrEmpty(str)) 144 { 145 DateTime.Parse(str); 146 return true; 147 } 148 else 149 { 150 return false; 151 } 152 } 153 catch 154 { 155 return false; 156 } 157 } 158 #endregion 159 160 #region 其他 161 162 /// <summary> 163 /// 检查字符串最大长度,返回指定长度的串 164 /// </summary> 165 /// <param name="sqlInput">输入字符串</param> 166 /// <param name="maxLength">最大长度</param> 167 /// <returns></returns> 168 public static string SqlText(string sqlInput, int maxLength) 169 { 170 if(sqlInput != null && sqlInput != string.Empty) 171 { 172 sqlInput = sqlInput.Trim(); 173 if(sqlInput.Length > maxLength)//按最大长度截取字符串 174 sqlInput = sqlInput.Substring(0, maxLength); 175 } 176 return sqlInput; 177 } 178 /// <summary> 179 /// 字符串编码 180 /// </summary> 181 /// <param name="inputData"></param> 182 /// <returns></returns> 183 public static string HtmlEncode(string inputData) 184 { 185 return HttpUtility.HtmlEncode(inputData); 186 } 187 /// <summary> 188 /// 设置Label显示Encode的字符串 189 /// </summary> 190 /// <param name="lbl"></param> 191 /// <param name="txtInput"></param> 192 public static void SetLabel(Label lbl, string txtInput) 193 { 194 lbl.Text = HtmlEncode(txtInput); 195 } 196 public static void SetLabel(Label lbl, object inputObj) 197 { 198 SetLabel(lbl, inputObj.ToString()); 199 } 200 //字符串清理 201 public static string InputText(string inputString, int maxLength) 202 { 203 StringBuilder retVal = new StringBuilder(); 204 205 // 检查是否为空 206 if ((inputString != null) && (inputString != String.Empty)) 207 { 208 inputString = inputString.Trim(); 209 210 //检查长度 211 if (inputString.Length > maxLength) 212 inputString = inputString.Substring(0, maxLength); 213 214 //替换危险字符 215 for (int i = 0; i < inputString.Length; i++) 216 { 217 switch (inputString[i]) 218 { 219 case '"': 220 retVal.Append("""); 221 break; 222 case '<': 223 retVal.Append("<"); 224 break; 225 case '>': 226 retVal.Append(">"); 227 break; 228 case '\'': 229 retVal.Append("");//Ankang Add 230 break; 231 default: 232 retVal.Append(inputString[i]); 233 break; 234 } 235 } 236 retVal.Replace("'", " ");// 替换单引号 237 } 238 return retVal.ToString(); 239 240 } 241 /// <summary>目前未使用 242 /// 过滤SQL语句,防止注入 243 /// </summary> 244 /// <param name="strSql"></param> 245 /// <returns>0 - 没有注入, 1 - 有注入 </returns> 246 public static int filterSql(string sSql) 247 { 248 int srcLen, decLen = 0; 249 sSql = sSql.ToLower().Trim(); 250 srcLen = sSql.Length; 251 sSql = sSql.Replace("exec", ""); 252 sSql = sSql.Replace("delete", ""); 253 sSql = sSql.Replace("master", ""); 254 sSql = sSql.Replace("truncate", ""); 255 sSql = sSql.Replace("declare", ""); 256 sSql = sSql.Replace("create", ""); 257 sSql = sSql.Replace("xp_", "no"); 258 decLen = sSql.Length; 259 if (srcLen == decLen) 260 return 0; 261 else return 1; 262 } 263 264 //字符串清理 265 public static string InputText(string inputString) 266 { 267 StringBuilder retVal = new StringBuilder(); 268 269 // 检查是否为空 270 if ((inputString != null) && (inputString != String.Empty)) 271 { 272 inputString = inputString.Trim(); 273 274 //检查长度 275 //if (inputString.Length > maxLength) 276 // inputString = inputString.Substring(0, maxLength); 277 278 //替换危险字符 279 for (int i = 0; i < inputString.Length; i++) 280 { 281 switch (inputString[i]) 282 { 283 case '"': 284 retVal.Append("""); 285 break; 286 case '<': 287 retVal.Append("<"); 288 break; 289 case '>': 290 retVal.Append(">"); 291 break; 292 default: 293 retVal.Append(inputString[i]); 294 break; 295 } 296 } 297 retVal.Replace("'", " ");// 替换单引号 298 } 299 return retVal.ToString(); 300 301 } 302 /// <summary> 303 /// 转换成 HTML code 304 /// </summary> 305 /// <param name="str">string</param> 306 /// <returns>string</returns> 307 public static string Encode(string str) 308 { 309 str = str.Replace("&","&"); 310 str = str.Replace("'","''"); 311 str = str.Replace("\"","""); 312 str = str.Replace(" "," "); 313 str = str.Replace("<","<"); 314 str = str.Replace(">",">"); 315 str = str.Replace("\n","<br>"); 316 return str; 317 } 318 /// <summary> 319 ///解析html成 普通文本 320 /// </summary> 321 /// <param name="str">string</param> 322 /// <returns>string</returns> 323 public static string Decode(string str) 324 { 325 str = str.Replace("<br>","\n"); 326 str = str.Replace(">",">"); 327 str = str.Replace("<","<"); 328 str = str.Replace(" "," "); 329 str = str.Replace(""","\""); 330 return str; 331 } 332 333 public static string SqlTextClear(string sqlText) 334 { 335 if (sqlText == null) 336 { 337 return null; 338 } 339 if (sqlText == "") 340 { 341 return ""; 342 } 343 sqlText = sqlText.Replace(",", "");//去除, 344 sqlText = sqlText.Replace("<", "");//去除< 345 sqlText = sqlText.Replace(">", "");//去除> 346 sqlText = sqlText.Replace("--", "");//去除-- 347 sqlText = sqlText.Replace("'", "");//去除' 348 sqlText = sqlText.Replace("\"", "");//去除" 349 sqlText = sqlText.Replace("=", "");//去除= 350 sqlText = sqlText.Replace("%", "");//去除% 351 sqlText = sqlText.Replace(" ", "");//去除空格 352 return sqlText; 353 } 354 355 #region ReadPost 解析POST内的Data数据 356 /// <summary> 357 /// 接收Post请求的Data数据处理 358 /// 2021-02-03 added by hyx 读取POST信息方式 359 /// </summary> 360 public static string ReadPost(System.IO.Stream inputStreamTemp) 361 { 362 var inputStream = inputStreamTemp; 363 string str = ""; 364 using (var sr = new System.IO.StreamReader(inputStream)) 365 str = sr.ReadToEnd(); 366 return str; 367 } 368 #endregion 369 370 #endregion 371 372 #region 是否由特定字符组成 373 public static bool isContainSameChar(string strInput) 374 { 375 string charInput = string.Empty; 376 if (!string.IsNullOrEmpty(strInput)) 377 { 378 charInput = strInput.Substring(0, 1); 379 } 380 return isContainSameChar(strInput, charInput, strInput.Length); 381 } 382 383 public static bool isContainSameChar(string strInput, string charInput, int lenInput) 384 { 385 if (string.IsNullOrEmpty(charInput)) 386 { 387 return false; 388 } 389 else 390 { 391 Regex RegNumber = new Regex(string.Format("^([{0}])+$", charInput)); 392 //Regex RegNumber = new Regex(string.Format("^([{0}]{{1}})+$", charInput,lenInput)); 393 Match m = RegNumber.Match(strInput); 394 return m.Success; 395 } 396 } 397 #endregion 398 399 #region 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查 400 /// <summary> 401 /// 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查 402 /// </summary> 403 public static bool isContainSpecChar(string strInput) 404 { 405 string[] list = new string[] { "123456", "654321" }; 406 bool result = new bool(); 407 for (int i = 0; i < list.Length; i++) 408 { 409 if (strInput == list[i]) 410 { 411 result = true; 412 break; 413 } 414 } 415 return result; 416 } 417 #endregion 418 419 public static string SafeLongFilter(string text, long defaultValue, char split = ',') 420 { 421 if (text.Trim().Length < 1) 422 return defaultValue.ToString(CultureInfo.InvariantCulture); 423 string[] tmpSplit = text.Split(new[] { split }, StringSplitOptions.RemoveEmptyEntries); 424 if (tmpSplit.Length < 1) 425 return defaultValue.ToString(CultureInfo.InvariantCulture); 426 427 long tmp; 428 for (int i = 0; i < tmpSplit.Length; i++) 429 { 430 if (long.TryParse(tmpSplit[i], out tmp)) 431 tmpSplit[i] = tmp.ToString(CultureInfo.InvariantCulture); 432 else 433 tmpSplit[i] = defaultValue.ToString(CultureInfo.InvariantCulture); 434 } 435 return string.Join(split.ToString(CultureInfo.InvariantCulture), tmpSplit); 436 } 437 438 public static String String2Json(String s) 439 { 440 StringBuilder sb = new StringBuilder(); 441 for (int i = 0; i < s.Length; i++) 442 { 443 char c = s.ToCharArray()[i]; 444 switch (c) 445 { 446 case '\"': 447 sb.Append("\\\""); break; 448 case '\\': 449 sb.Append("\\\\"); break; 450 case '/': 451 sb.Append("\\/"); break; 452 case '\b': 453 sb.Append("\\b"); break; 454 case '\f': 455 sb.Append("\\f"); break; 456 case '\n': 457 sb.Append("\\n"); break; 458 case '\r': 459 sb.Append("\\r"); break; 460 case '\t': 461 sb.Append("\\t"); break; 462 default: 463 if ((c >= 0 && c <= 31) || c == 127)//在ASCⅡ码中,第0~31号及第127号(共33个)是控制字符或通讯专用字符 464 { 465 466 } 467 else 468 { 469 sb.Append(c); 470 } 471 break; 472 } 473 } 474 return sb.ToString(); 475 } 476 } 477 }
后面会整理到Fluentvalidation中。
本文来自博客园,作者:꧁执笔小白꧂,转载请注明原文链接:https://www.cnblogs.com/qq2806933146xiaobai/p/15826415.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
2021-01-20 Android的Looper.loop()消息循环机制
2021-01-20 申请读写sd卡权限shell
2021-01-20 Linux系统知识(四)-Shell脚本学习笔记
2021-01-20 Android Studio常用快捷方式
2021-01-20 Android ContentProvider操作其他应用程序共享的数据