CodeHelper.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace Ant.Service.Common
  7. {
  8. /// <summary>
  9. /// 功能描述:部门编号帮助类
  10. /// 编号组成规则:编号规则为3位数字/组 如:山东广域001 行政办001001 财务001002 济南广域001003
  11. /// 验证规则:1:对3取余数=0 2:可转换成数字
  12. /// 创建标识:add by 季健国 2013-7-17 10:19
  13. /// </summary>
  14. public class CodeHelper
  15. {
  16. /// <summary>
  17. /// 判断手机号码
  18. /// </summary>
  19. /// <param name="phoneNo"></param>
  20. /// <returns></returns>
  21. public static bool IsPhoneNo(string phoneNo)
  22. {
  23. string str = @"^0{0,1}(13[0-9]|15[0-9]|17[0-9]|18[0-9])[0-9]{8}$";
  24. return Regex.IsMatch(phoneNo, str);
  25. }
  26. #region 验证身份证
  27. public static bool CheckIDCard(string Id)
  28. {
  29. if (Id.Length == 18)
  30. {
  31. bool check = CheckIDCard18(Id);
  32. return check;
  33. }
  34. else if (Id.Length == 15)
  35. {
  36. bool check = CheckIDCard15(Id);
  37. return check;
  38. }
  39. else
  40. {
  41. return false;
  42. }
  43. }
  44. public static bool CheckIDCard18(string Id)
  45. {
  46. long n = 0;
  47. if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
  48. {
  49. return false;//数字验证
  50. }
  51. string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  52. if (address.IndexOf(Id.Remove(2)) == -1)
  53. {
  54. return false;//省份验证
  55. }
  56. string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
  57. DateTime time = new DateTime();
  58. if (DateTime.TryParse(birth, out time) == false)
  59. {
  60. return false;//生日验证
  61. }
  62. string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
  63. string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
  64. char[] Ai = Id.Remove(17).ToCharArray();
  65. int sum = 0;
  66. for (int i = 0; i < 17; i++)
  67. {
  68. sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
  69. }
  70. int y = -1;
  71. DivRem(sum, 11, out y);
  72. if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
  73. {
  74. return false;//校验码验证
  75. }
  76. return true;//符合GB11643-1999标准
  77. }
  78. public static int DivRem(int a, int b, out int result)
  79. {
  80. result = a % b;
  81. return (a / b);
  82. }
  83. public static bool CheckIDCard15(string Id)
  84. {
  85. long n = 0;
  86. if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
  87. {
  88. return false;//数字验证
  89. }
  90. string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  91. if (address.IndexOf(Id.Remove(2)) == -1)
  92. {
  93. return false;//省份验证
  94. }
  95. string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
  96. DateTime time = new DateTime();
  97. if (DateTime.TryParse(birth, out time) == false)
  98. {
  99. return false;//生日验证
  100. }
  101. return true;//符合15位身份证标准
  102. }
  103. #endregion
  104. /// <summary>
  105. /// 功能描述:编号验证
  106. /// 验证规则:1.不为空 2.对3取余数=0 3.可转化成数字
  107. /// 创建标识:add by 季健国 2013-7-17 10:37
  108. /// </summary>
  109. /// <param name="strCode">需要验证的编号</param>
  110. /// <returns>true=是标准编号 false=不是标准编号</returns>
  111. public static bool ValidateCode(string strCode)
  112. {
  113. try
  114. {
  115. //step1:验证是否为空
  116. if (string.IsNullOrEmpty(strCode))
  117. return false;
  118. //step2:验证 编号是否标准
  119. int _intYuShu = strCode.Length % 3;//对3取余数 验证
  120. if (_intYuShu != 0)
  121. return false;
  122. if (Int32.TryParse(strCode, out _intYuShu) == false) //转化数字验证
  123. return false;
  124. return true;
  125. }
  126. catch (Exception ex)
  127. {
  128. throw ex;
  129. }
  130. }
  131. #region 检测是否有Sql危险字符
  132. /// <summary>
  133. /// 检测是否有Sql危险字符
  134. /// </summary>
  135. /// <param name="str">要判断字符串</param>
  136. /// <returns>判断结果</returns>
  137. public static bool IsSafeSqlString(string str)
  138. {
  139. return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
  140. }
  141. /// <summary>
  142. /// 检查危险字符
  143. /// </summary>
  144. /// <param name="Input"></param>
  145. /// <returns></returns>
  146. public static string Filter(string sInput)
  147. {
  148. if (sInput == null || sInput == "")
  149. return null;
  150. string sInput1 = sInput.ToLower();
  151. string output = sInput;
  152. string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
  153. if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
  154. {
  155. throw new Exception("字符串中含有非法字符!");
  156. }
  157. else
  158. {
  159. output = output.Replace("'", "''");
  160. }
  161. return output;
  162. }
  163. /// <summary>
  164. /// 检查过滤设定的危险字符
  165. /// </summary>
  166. /// <param name="InText">要过滤的字符串 </param>
  167. /// <returns>如果参数存在不安全字符,则返回true </returns>
  168. public static bool SqlFilter(string word, string InText)
  169. {
  170. if (InText == null)
  171. return false;
  172. foreach (string i in word.Split('|'))
  173. {
  174. if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
  175. {
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. #endregion
  182. //编号自增长
  183. public static string CreateCode(string parentCode, int childIndex)
  184. {
  185. string _strCode = string.Empty;
  186. //是否为顶级部门编号 需要查询数据是否存在数据,如果不存在则为顶级部门编号
  187. //不是顶级部门编号 需要上级部门编号+查询数据库下级部门的个数
  188. //如果中间删除了某一个部门,此部门编号在新增部门时可用
  189. return _strCode;
  190. }
  191. //
  192. /// <summary>
  193. /// 功能描述:根据传入编号获取上级部门编号
  194. /// 验证规则:1.调用ValidateCode函数 2.验证是否存在上级部门
  195. /// 创建标识:add by 季健国 2013-7-17 10:49
  196. /// </summary>
  197. /// <param name="strChildCode"></param>
  198. /// <param name="strParentCode"></param>
  199. public static void GetParentCode(string strChildCode, ref string strParentCode)
  200. {
  201. try
  202. {
  203. strParentCode = string.Empty;
  204. //调用验证方法验证编号
  205. if (ValidateCode(strChildCode))
  206. {
  207. //验证通过
  208. //验证是否有上级单位
  209. if (strChildCode.Length == 3)
  210. {
  211. //没有上级单位
  212. strParentCode = string.Empty;
  213. }
  214. else
  215. {
  216. //有上级单位
  217. strParentCode = strChildCode.Substring(0, strChildCode.Length - 3);
  218. }
  219. }
  220. else
  221. throw new ArgumentException("编号验证未通过!");
  222. }
  223. catch (Exception ex)
  224. {
  225. throw ex;
  226. }
  227. }
  228. }
  229. }