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 }
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
- 301.
- 302.
- 303.
- 304.
- 305.
- 306.
- 307.
- 308.
- 309.
- 310.
- 311.
- 312.
- 313.
- 314.
- 315.
- 316.
- 317.
- 318.
- 319.
- 320.
- 321.
- 322.
- 323.
- 324.
- 325.
- 326.
- 327.
- 328.
- 329.
- 330.
- 331.
- 332.
- 333.
- 334.
- 335.
- 336.
- 337.
- 338.
- 339.
- 340.
- 341.
- 342.
- 343.
- 344.
- 345.
- 346.
- 347.
- 348.
- 349.
- 350.
- 351.
- 352.
- 353.
- 354.
- 355.
- 356.
- 357.
- 358.
- 359.
- 360.
- 361.
- 362.
- 363.
- 364.
- 365.
- 366.
- 367.
- 368.
- 369.
- 370.
- 371.
- 372.
- 373.
- 374.
- 375.
- 376.
- 377.
- 378.
- 379.
- 380.
- 381.
- 382.
- 383.
- 384.
- 385.
- 386.
- 387.
- 388.
- 389.
- 390.
- 391.
- 392.
- 393.
- 394.
- 395.
- 396.
- 397.
- 398.
- 399.
- 400.
- 401.
- 402.
- 403.
- 404.
- 405.
- 406.
- 407.
- 408.
- 409.
- 410.
- 411.
- 412.
- 413.
- 414.
- 415.
- 416.
- 417.
- 418.
- 419.
- 420.
- 421.
- 422.
- 423.
- 424.
- 425.
- 426.
- 427.
- 428.
- 429.
- 430.
- 431.
- 432.
- 433.
- 434.
- 435.
- 436.
- 437.
- 438.
- 439.
- 440.
- 441.
- 442.
- 443.
- 444.
- 445.
- 446.
- 447.
- 448.
- 449.
- 450.
- 451.
- 452.
- 453.
- 454.
- 455.
- 456.
- 457.
- 458.
- 459.
- 460.
- 461.
- 462.
- 463.
- 464.
- 465.
- 466.
- 467.
- 468.
- 469.
- 470.
- 471.
- 472.
- 473.
- 474.
- 475.
- 476.
- 477.
后面会整理到Fluentvalidation中。
作者:꧁执笔小白꧂