using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace Ant.Service.Common { /// /// 功能描述:部门编号帮助类 /// 编号组成规则:编号规则为3位数字/组 如:山东广域001 行政办001001 财务001002 济南广域001003 /// 验证规则:1:对3取余数=0 2:可转换成数字 /// 创建标识:add by 季健国 2013-7-17 10:19 /// public class CodeHelper { /// /// 判断手机号码 /// /// /// 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 /// /// 功能描述:编号验证 /// 验证规则:1.不为空 2.对3取余数=0 3.可转化成数字 /// 创建标识:add by 季健国 2013-7-17 10:37 /// /// 需要验证的编号 /// true=是标准编号 false=不是标准编号 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危险字符 /// /// 检测是否有Sql危险字符 /// /// 要判断字符串 /// 判断结果 public static bool IsSafeSqlString(string str) { return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"); } /// /// 检查危险字符 /// /// /// 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; } /// /// 检查过滤设定的危险字符 /// /// 要过滤的字符串 /// 如果参数存在不安全字符,则返回true 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; } // /// /// 功能描述:根据传入编号获取上级部门编号 /// 验证规则:1.调用ValidateCode函数 2.验证是否存在上级部门 /// 创建标识:add by 季健国 2013-7-17 10:49 /// /// /// 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; } } } }