123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace Ant.Service.Common
- {
- /// <summary>
- /// 功能描述:部门编号帮助类
- /// 编号组成规则:编号规则为3位数字/组 如:山东广域001 行政办001001 财务001002 济南广域001003
- /// 验证规则:1:对3取余数=0 2:可转换成数字
- /// 创建标识:add by 季健国 2013-7-17 10:19
- /// </summary>
- public class CodeHelper
- {
- /// <summary>
- /// 判断手机号码
- /// </summary>
- /// <param name="phoneNo"></param>
- /// <returns></returns>
- public static bool IsPhoneNo(string phoneNo)
- {
- string str = @"^0{0,1}(13[0-9]|15[0-9]|17[0-9]|18[0-9])[0-9]{8}$";
- return Regex.IsMatch(phoneNo, str);
- }
- #region 验证身份证
- public static bool CheckIDCard(string Id)
- {
- if (Id.Length == 18)
- {
- bool check = CheckIDCard18(Id);
- return check;
- }
- else if (Id.Length == 15)
- {
- bool check = CheckIDCard15(Id);
- return check;
- }
- else
- {
- return false;
- }
- }
- public static bool CheckIDCard18(string Id)
- {
- long n = 0;
- 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)
- {
- return false;//数字验证
- }
- string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
- if (address.IndexOf(Id.Remove(2)) == -1)
- {
- return false;//省份验证
- }
- string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
- DateTime time = new DateTime();
- if (DateTime.TryParse(birth, out time) == false)
- {
- return false;//生日验证
- }
- string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
- string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
- char[] Ai = Id.Remove(17).ToCharArray();
- int sum = 0;
- for (int i = 0; i < 17; i++)
- {
- sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
- }
- int y = -1;
- DivRem(sum, 11, out y);
- if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
- {
- return false;//校验码验证
- }
- return true;//符合GB11643-1999标准
- }
- public static int DivRem(int a, int b, out int result)
- {
- result = a % b;
- return (a / b);
- }
- public static bool CheckIDCard15(string Id)
- {
- long n = 0;
- if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
- {
- return false;//数字验证
- }
- string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
- if (address.IndexOf(Id.Remove(2)) == -1)
- {
- return false;//省份验证
- }
- string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
- DateTime time = new DateTime();
- if (DateTime.TryParse(birth, out time) == false)
- {
- return false;//生日验证
- }
- return true;//符合15位身份证标准
- }
- #endregion
- /// <summary>
- /// 功能描述:编号验证
- /// 验证规则:1.不为空 2.对3取余数=0 3.可转化成数字
- /// 创建标识:add by 季健国 2013-7-17 10:37
- /// </summary>
- /// <param name="strCode">需要验证的编号</param>
- /// <returns>true=是标准编号 false=不是标准编号</returns>
- public static bool ValidateCode(string strCode)
- {
- try
- {
- //step1:验证是否为空
- if (string.IsNullOrEmpty(strCode))
- return false;
- //step2:验证 编号是否标准
- int _intYuShu = strCode.Length % 3;//对3取余数 验证
- if (_intYuShu != 0)
- return false;
- if (Int32.TryParse(strCode, out _intYuShu) == false) //转化数字验证
- return false;
- return true;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #region 检测是否有Sql危险字符
- /// <summary>
- /// 检测是否有Sql危险字符
- /// </summary>
- /// <param name="str">要判断字符串</param>
- /// <returns>判断结果</returns>
- public static bool IsSafeSqlString(string str)
- {
- return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
- }
- /// <summary>
- /// 检查危险字符
- /// </summary>
- /// <param name="Input"></param>
- /// <returns></returns>
- public static string Filter(string sInput)
- {
- if (sInput == null || sInput == "")
- return null;
- string sInput1 = sInput.ToLower();
- string output = sInput;
- string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
- if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
- {
- throw new Exception("字符串中含有非法字符!");
- }
- else
- {
- output = output.Replace("'", "''");
- }
- return output;
- }
- /// <summary>
- /// 检查过滤设定的危险字符
- /// </summary>
- /// <param name="InText">要过滤的字符串 </param>
- /// <returns>如果参数存在不安全字符,则返回true </returns>
- public static bool SqlFilter(string word, string InText)
- {
- if (InText == null)
- return false;
- foreach (string i in word.Split('|'))
- {
- if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
- {
- return true;
- }
- }
- return false;
- }
- #endregion
- //编号自增长
- public static string CreateCode(string parentCode, int childIndex)
- {
- string _strCode = string.Empty;
- //是否为顶级部门编号 需要查询数据是否存在数据,如果不存在则为顶级部门编号
- //不是顶级部门编号 需要上级部门编号+查询数据库下级部门的个数
- //如果中间删除了某一个部门,此部门编号在新增部门时可用
- return _strCode;
- }
- //
- /// <summary>
- /// 功能描述:根据传入编号获取上级部门编号
- /// 验证规则:1.调用ValidateCode函数 2.验证是否存在上级部门
- /// 创建标识:add by 季健国 2013-7-17 10:49
- /// </summary>
- /// <param name="strChildCode"></param>
- /// <param name="strParentCode"></param>
- public static void GetParentCode(string strChildCode, ref string strParentCode)
- {
- try
- {
- strParentCode = string.Empty;
- //调用验证方法验证编号
- if (ValidateCode(strChildCode))
- {
- //验证通过
- //验证是否有上级单位
- if (strChildCode.Length == 3)
- {
- //没有上级单位
- strParentCode = string.Empty;
- }
- else
- {
- //有上级单位
- strParentCode = strChildCode.Substring(0, strChildCode.Length - 3);
- }
- }
- else
- throw new ArgumentException("编号验证未通过!");
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
|