ValidUtils.cs 13 KB

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