Command_Validate.cs 12 KB

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