• .NET身份证验证


    身份证号码编码规则及校验位校验算法

    算法地址:http://jingyan.baidu.com/article/7f41ececff944a593d095c8c.html

    简单验证长度

     1         /// <summary>
     2         /// 检查身份证基本信息
     3         /// </summary>
     4         /// <returns>结果</returns>
     5         private string CheckAndParse()
     6         {
     7             if (string.IsNullOrWhiteSpace(this.Id))
     8             {
     9                 return "身份证号码不能为空";
    10             }
    11             if (this.Id.Length == 15)
    12             {
    13                 return this.ParseCardInfo15();
    14             }
    15             if (this.Id.Length == 18)
    16             {
    17                 return this.ParseCardInfo18();
    18             }
    19             return "身份证号码必须为15位或18位";
    20         }
    简单验证

     身份证分为2中 18位 OR 15位

     验证先用正则表达式进行初步验证 并且取出 身份证上面包含的信息(地区 生日 随机码 性别 尾号)

    18位身份证

            /// <summary>
            /// 18位身份证
            /// </summary>
            /// <returns>结果</returns>
            private string ParseCardInfo18()
            {
                const string CardIdParttern = @"(d{6})(d{4})(d{2})(d{2})(d{2})(d{1})([d,x,X]{1})";
                Match match = Regex.Match(this.Id, CardIdParttern);
                if (match.Success)
                {
                    this.areaCode = match.Groups[1].Value;
                    string year = match.Groups[2].Value;
                    string month = match.Groups[3].Value;
                    string day = match.Groups[4].Value;
                    this.birthCode = year + month + day;
                    this.randomCode = match.Groups[5].Value;
                    this.sexCode = char.Parse(match.Groups[6].Value);
                    string verifyCode = match.Groups[7].Value.ToUpper();
    
                    if (this.ValidateVerifyCode(this.Id.Substring(0, 17), char.Parse(verifyCode)))
                    {
                        try
                        {
                            this.Birth = BirthDate.GetBirthDate(year, month, day);
                            this.Area = AreaCodeMapping.GetArea(this.areaCode);
                            Sex = GetSex(this.sexCode);
                        }
                        catch (System.Exception ex)
                        {
                            return ex.Message;
                        }
                        return string.Empty;
                    }
                }
                return "身份证号码格式错误";
            }
    18位

    15位身份证

     1         /// <summary>
     2         /// 15位身份证
     3         /// </summary>
     4         /// <returns>结果</returns>
     5         private string ParseCardInfo15()
     6         {
     7             const string CardIdParttern = @"(d{6})(d{2})(d{2})(d{2})(d{2})(d{1})";
     8             Match match = Regex.Match(this.Id, CardIdParttern);
     9             if (match.Success)
    10             {
    11                 this.areaCode = match.Groups[1].Value;
    12                 string year = match.Groups[2].Value;
    13                 string month = match.Groups[3].Value;
    14                 string day = match.Groups[4].Value;
    15                 this.birthCode = year + month + day;
    16                 this.randomCode = match.Groups[5].Value;
    17                 this.sexCode = char.Parse(match.Groups[6].Value);
    18 
    19                 try
    20                 {
    21                     this.Area = AreaCodeMapping.GetArea(this.areaCode);
    22                     this.Birth = BirthDate.GetBirthDate(year, month, day);
    23                     Sex = GetSex(this.sexCode);
    24                 }
    25                 catch (System.Exception ex)
    26                 {
    27                     return ex.Message;
    28                 }
    29                 return string.Empty;
    30             }
    31             return "身份证号码格式错误";
    32         }
    15位

    校验代码

      1 namespace Notify.Solution.Code.IdentityCard
      2 {
      3     /// <summary>
      4     /// 验证类
      5     /// </summary>
      6     public class Validator
      7     {
      8         /// <summary>
      9         /// 验证码
     10         /// </summary>
     11         private readonly char[] verifyCodeMapping = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
     12 
     13         /// <summary>
     14         /// 地区代码
     15         /// </summary>
     16         private string areaCode;
     17 
     18         /// <summary>
     19         /// 生日
     20         /// </summary>
     21         private string birthCode;
     22 
     23         /// <summary>
     24         /// 随机
     25         /// </summary>
     26         private string randomCode;
     27 
     28         /// <summary>
     29         /// 性别
     30         /// </summary>
     31         private char sexCode;
     32 
     33         /// <summary>
     34         /// Initializes a new instance of the <see cref="Validator"/> class. 
     35         /// </summary>
     36         /// <param name="id">Id</param>
     37         public Validator(string id)
     38         {
     39             this.Id = id;
     40             this.Success = false;
     41             this.ErrorMessage = string.Empty;
     42             this.Area = null;
     43             this.Birth = BirthDate.Empty;
     44             this.Sex = Sex.Male;
     45         }
     46 
     47         /// <summary>
     48         /// 身份证ID
     49         /// </summary>
     50         public string Id { get; private set; }
     51 
     52         /// <summary>
     53         /// 结果
     54         /// </summary>
     55         public bool Success { get; private set; }
     56 
     57         /// <summary>
     58         /// 错误信息
     59         /// </summary>
     60         public string ErrorMessage { get; private set; }
     61 
     62         /// <summary>
     63         ///  区域信息
     64         /// </summary>
     65         public AreaInformation Area { get; private set; }
     66 
     67         /// <summary>
     68         ///  生日
     69         /// </summary>
     70         public BirthDate Birth { get; private set; }
     71 
     72         /// <summary>
     73         /// 性别
     74         /// </summary>
     75         public Sex Sex { get; private set; }
     76 
     77         /// <summary>
     78         /// 执行比较结果
     79         /// </summary>
     80         /// <returns>结果</returns>
     81         public bool Execute()
     82         {
     83             string msg = this.CheckAndParse();
     84             if (string.IsNullOrWhiteSpace(msg))
     85             {
     86                 this.ErrorMessage = string.Empty;
     87                 this.Success = true;
     88             }
     89             else
     90             {
     91                 this.ErrorMessage = msg;
     92                 this.Success = false;
     93             }
     94             return this.Success;
     95         }
     96 
     97         /// <summary>
     98         /// IdentityCard18
     99         /// </summary>
    100         public string IdentityCard18
    101         {
    102             get
    103             {
    104                 if (string.IsNullOrWhiteSpace(this.Id))
    105                 {
    106                     return "身份证号码不能为空";
    107                 }
    108                 if (this.Success && this.Id.Length == 15)
    109                 {
    110                     return this.ToCardInfo18();
    111                 }
    112                 return this.Id;
    113             }
    114         }
    115 
    116         /// <summary>
    117         /// ToCardInfo18
    118         /// </summary>
    119         /// <returns>结果</returns>
    120         private string ToCardInfo18()
    121         {
    122             string bodyCode = GetBodyCode(this.areaCode, "19" + this.birthCode, this.randomCode, this.sexCode);
    123             char verifyCode = this.GetVerifyCode(bodyCode);
    124             return bodyCode + verifyCode;
    125         }
    126 
    127         /// <summary>
    128         /// 获取bodyCode
    129         /// </summary>
    130         /// <param name="areaCode">areaCode</param>
    131         /// <param name="birthCode">birthCode</param>
    132         /// <param name="randomCode">randomCode</param>
    133         /// <param name="sexCode">sexCode</param>
    134         /// <returns></returns>
    135         private static string GetBodyCode(string areaCode, string birthCode, string randomCode, char sexCode)
    136         {
    137             return areaCode + birthCode + randomCode + sexCode.ToString();
    138         }
    139 
    140         /// <summary>
    141         /// 检查身份证基本信息
    142         /// </summary>
    143         /// <returns>结果</returns>
    144         private string CheckAndParse()
    145         {
    146             if (string.IsNullOrWhiteSpace(this.Id))
    147             {
    148                 return "身份证号码不能为空";
    149             }
    150             if (this.Id.Length == 15)
    151             {
    152                 return this.ParseCardInfo15();
    153             }
    154             if (this.Id.Length == 18)
    155             {
    156                 return this.ParseCardInfo18();
    157             }
    158             return "身份证号码必须为15位或18位";
    159         }
    160 
    161         /// <summary>
    162         /// 18位身份证
    163         /// </summary>
    164         /// <returns>结果</returns>
    165         private string ParseCardInfo18()
    166         {
    167             const string CardIdParttern = @"(d{6})(d{4})(d{2})(d{2})(d{2})(d{1})([d,x,X]{1})";
    168             Match match = Regex.Match(this.Id, CardIdParttern);
    169             if (match.Success)
    170             {
    171                 this.areaCode = match.Groups[1].Value;
    172                 string year = match.Groups[2].Value;
    173                 string month = match.Groups[3].Value;
    174                 string day = match.Groups[4].Value;
    175                 this.birthCode = year + month + day;
    176                 this.randomCode = match.Groups[5].Value;
    177                 this.sexCode = char.Parse(match.Groups[6].Value);
    178                 string verifyCode = match.Groups[7].Value.ToUpper();
    179 
    180                 if (this.ValidateVerifyCode(this.Id.Substring(0, 17), char.Parse(verifyCode)))
    181                 {
    182                     try
    183                     {
    184                         this.Birth = BirthDate.GetBirthDate(year, month, day);
    185                         this.Area = AreaCodeMapping.GetArea(this.areaCode);
    186                         Sex = GetSex(this.sexCode);
    187                     }
    188                     catch (System.Exception ex)
    189                     {
    190                         return ex.Message;
    191                     }
    192                     return string.Empty;
    193                 }
    194             }
    195             return "身份证号码格式错误";
    196         }
    197 
    198         /// <summary>
    199         /// 验证验证码
    200         /// </summary>
    201         /// <param name="bodyCode">bodyCode</param>
    202         /// <param name="verifyCode">verifyCode</param>
    203         /// <returns>结果</returns>
    204         private bool ValidateVerifyCode(string bodyCode, char verifyCode)
    205         {
    206             char calculatedVerifyCode = this.GetVerifyCode(bodyCode);
    207             return calculatedVerifyCode == verifyCode;
    208         }
    209 
    210         /// <summary>
    211         /// 15位身份证
    212         /// </summary>
    213         /// <returns>结果</returns>
    214         private string ParseCardInfo15()
    215         {
    216             const string CardIdParttern = @"(d{6})(d{2})(d{2})(d{2})(d{2})(d{1})";
    217             Match match = Regex.Match(this.Id, CardIdParttern);
    218             if (match.Success)
    219             {
    220                 this.areaCode = match.Groups[1].Value;
    221                 string year = match.Groups[2].Value;
    222                 string month = match.Groups[3].Value;
    223                 string day = match.Groups[4].Value;
    224                 this.birthCode = year + month + day;
    225                 this.randomCode = match.Groups[5].Value;
    226                 this.sexCode = char.Parse(match.Groups[6].Value);
    227 
    228                 try
    229                 {
    230                     this.Area = AreaCodeMapping.GetArea(this.areaCode);
    231                     this.Birth = BirthDate.GetBirthDate(year, month, day);
    232                     Sex = GetSex(this.sexCode);
    233                 }
    234                 catch (System.Exception ex)
    235                 {
    236                     return ex.Message;
    237                 }
    238                 return string.Empty;
    239             }
    240             return "身份证号码格式错误";
    241         }
    242 
    243         /// <summary>
    244         /// 获取验证码
    245         /// </summary>
    246         /// <param name="bodyCode">bodyCode</param>
    247         /// <returns>结果</returns>
    248         private char GetVerifyCode(string bodyCode)
    249         {
    250             char[] bodyCodeArray = bodyCode.ToCharArray();
    251             ////int sum = 0;
    252             ////for (int index = 0; index < bodyCodeArray.Length; index++)
    253             ////{
    254             ////    sum += int.Parse(bodyCodeArray[index].ToString()) * GetWeight(index);
    255             ////}
    256             ////return this.verifyCodeMapping[sum % 11];
    257             int sum = bodyCodeArray.Select((t, index) => int.Parse(t.ToString()) * GetWeight(index)).Sum();
    258             return this.verifyCodeMapping[sum % 11];
    259         }
    260 
    261         /// <summary>
    262         /// GetWeight
    263         /// </summary>
    264         /// <param name="index">index</param>
    265         /// <returns>index</returns>
    266         private static int GetWeight(int index)
    267         {
    268             return (1 << (17 - index)) % 11;
    269         }
    270 
    271         /// <summary>
    272         /// 获取性别
    273         /// </summary>
    274         /// <param name="sexCode">性别代码</param>
    275         /// <returns>性别</returns>
    276         private static Sex GetSex(char sexCode)
    277         {
    278             return ((int)sexCode) % 2 == 0 ? Sex.Female : Sex.Male;
    279         }
    280     }
    281 
    282     /// <summary>
    283     /// 生日
    284     /// </summary>
    285     public struct BirthDate
    286     {
    287         /// <summary>
    288         ///289         /// </summary>
    290         private readonly string year;
    291 
    292         /// <summary>
    293         ///294         /// </summary>
    295         private readonly string month;
    296 
    297         /// <summary>
    298         ///299         /// </summary>
    300         private readonly string day;
    301 
    302         /// <summary>
    303         /// 默认
    304         /// </summary>
    305         public static BirthDate Empty
    306         {
    307             get { return new BirthDate("00", "00", "00"); }
    308         }
    309 
    310         /// <summary>
    311         /// Initializes a new instance of the <see cref="BirthDate"/> struct. 
    312         /// </summary>
    313         /// <param name="year"></param>
    314         /// <param name="month"></param>
    315         /// <param name="day"></param>
    316         public BirthDate(string year, string month, string day)
    317         {
    318             this.year = year;
    319             this.month = month;
    320             this.day = day;
    321         }
    322 
    323         /// <summary>
    324         /// 获取生日
    325         /// </summary>
    326         /// <param name="year"></param>
    327         /// <param name="month"></param>
    328         /// <param name="day"></param>
    329         /// <returns>结果</returns>
    330         public static BirthDate GetBirthDate(string year, string month, string day)
    331         {
    332             DateTime date;
    333             if (DateTime.TryParse(string.Format("{0}-{1}-{2}", year, month, day), out date))
    334             {
    335                 return new BirthDate(year, month, day);
    336             }
    337             throw new System.Exception("日期不存在");
    338         }
    339 
    340         /// <summary>
    341         ///342         /// </summary>
    343         public string Year
    344         {
    345             get { return this.year; }
    346         }
    347 
    348         /// <summary>
    349         ///350         /// </summary>
    351         public string Month
    352         {
    353             get { return this.month; }
    354         }
    355 
    356         /// <summary>
    357         ///358         /// </summary>
    359         public string Day
    360         {
    361             get { return this.day; }
    362         }
    363 
    364         /// <summary>
    365         /// 重写ToString
    366         /// </summary>
    367         /// <returns>结果</returns>
    368         public override string ToString()
    369         {
    370             return string.Format("{0}年{1}月{2}日", this.year, this.month, this.day);
    371         }
    372 
    373         /// <summary>
    374         /// 重写ToString
    375         /// </summary>
    376         /// <param name="separator">separator</param>
    377         /// <returns>结果</returns>
    378         public string ToString(string separator)
    379         {
    380             return string.Format("{1}{0}{2}{0}{3}", separator, this.year, this.month, this.day);
    381         }
    382     }
    383 
    384     /// <summary>
    385     /// 性别
    386     /// </summary>
    387     public enum Sex
    388     {
    389         /// <summary>
    390         ///391         /// </summary>
    392         Male,
    393 
    394         /// <summary>
    395         ///396         /// </summary>
    397         Female
    398     }
    399 }
    View Code

    读取地区代码

      1 namespace Notify.Solution.Code.IdentityCard
      2 {
      3     /// <summary>
      4     /// 区域配置
      5     /// </summary>
      6     public class AreaCodeMapping
      7     {
      8         /// <summary>
      9         /// 区域字典
     10         /// </summary>
     11         private static readonly Dictionary<string, Area> areas;
     12 
     13         /// <summary>
     14         /// Initializes static members of the <see cref="AreaCodeMapping"/> class. 
     15         /// </summary>
     16         static AreaCodeMapping()
     17         {
     18             areas = LoadAreaInfo();
     19         }
     20 
     21         /// <summary>
     22         /// 加载信息
     23         /// </summary>
     24         /// <returns>区域信息</returns>
     25         private static Dictionary<string, Area> LoadAreaInfo()
     26         {
     27             XmlDocument doc = LoadXmlDocument("AreaCodes.xml");
     28             XmlNode areasNode = doc.SelectSingleNode("AreaCode");
     29             if (areasNode != null)
     30             {
     31                 XmlNodeList provinceNodeList = areasNode.ChildNodes;
     32                 return LoadProvinces(provinceNodeList);
     33             }
     34 
     35             return null;
     36         }
     37 
     38         /// <summary>
     39         /// 加载XML
     40         /// </summary>
     41         /// <param name="fileName">文件名</param>
     42         /// <returns>XmlDocument</returns>
     43         private static XmlDocument LoadXmlDocument(string fileName)
     44         {
     45             var declaringType = MethodBase.GetCurrentMethod().DeclaringType;
     46             if (declaringType != null)
     47             {
     48                 string resourceName = declaringType.Namespace + "." + fileName;
     49                 Assembly assembly = Assembly.GetExecutingAssembly();
     50                 Stream stream = assembly.GetManifestResourceStream(resourceName);
     51                 XmlDocument result = new XmlDocument();
     52                 if (stream != null)
     53                 {
     54                     result.Load(stream);
     55                 }
     56                 return result;
     57             }
     58             return null;
     59         }
     60 
     61         /// <summary>
     62         /// 解析XML节点
     63         /// </summary>
     64         /// <param name="provinceNodeList">provinceNodeList</param>
     65         /// <returns>结果</returns>
     66         private static Dictionary<string, Area> LoadProvinces(XmlNodeList provinceNodeList)
     67         {
     68             Dictionary<string, Area> result = new Dictionary<string, Area>();
     69             foreach (XmlNode provinceNode in provinceNodeList)
     70             {
     71                 string code = GetAttribute(provinceNode, "code");
     72                 string name = GetAttribute(provinceNode, "name");
     73                 Area province = new Area(code, name, null);
     74                 var cities = LoadCities(province, provinceNode.ChildNodes);
     75                 foreach (var city in cities)
     76                 {
     77                     province.AppendChild(city);
     78                 }
     79                 result.Add(code, province);
     80             }
     81             return result;
     82         }
     83 
     84         /// <summary>
     85         /// 加载城市
     86         /// </summary>
     87         /// <param name="province"></param>
     88         /// <param name="cityNodeList">节点</param>
     89         /// <returns>结果</returns>
     90         private static IEnumerable<Area> LoadCities(Area province, XmlNodeList cityNodeList)
     91         {
     92             List<Area> result = new List<Area>();
     93             if (cityNodeList != null)
     94             {
     95                 foreach (XmlNode cityNode in cityNodeList)
     96                 {
     97                     string code = GetAttribute(cityNode, "code");
     98                     string name = GetAttribute(cityNode, "name");
     99                     Area city = new Area(code, name, province);
    100                     var counties = loadCounties(city, cityNode.ChildNodes);
    101                     foreach (var county in counties)
    102                     {
    103                         city.AppendChild(county);
    104                     }
    105                     result.Add(city);
    106                 }
    107             }
    108             return result;
    109         }
    110 
    111         /// <summary>
    112         /// 加载区域
    113         /// </summary>
    114         /// <param name="city"></param>
    115         /// <param name="countyNodeList">节点</param>
    116         /// <returns>结果</returns>
    117         private static IEnumerable<Area> loadCounties(Area city, XmlNodeList countyNodeList)
    118         {
    119             List<Area> result = new List<Area>();
    120             if (countyNodeList != null)
    121             {
    122                 foreach (XmlNode countyNode in countyNodeList)
    123                 {
    124                     string code = GetAttribute(countyNode, "code");
    125                     string name = GetAttribute(countyNode, "name");
    126                     Area county = new Area(code, name, city);
    127                     result.Add(county);
    128                 }
    129             }
    130             return result;
    131         }
    132 
    133         /// <summary>
    134         /// 获取节点属性
    135         /// </summary>
    136         /// <param name="node">node</param>
    137         /// <param name="attributeName">attributeName</param>
    138         /// <returns>结果</returns>
    139         private static string GetAttribute(XmlNode node, string attributeName)
    140         {
    141             if (node.Attributes != null)
    142             {
    143                 XmlAttribute attribute = node.Attributes[attributeName];
    144                 return attribute == null ? string.Empty : attribute.Value;
    145             }
    146             return string.Empty;
    147         }
    148 
    149         /// <summary>
    150         /// 获取区域信息
    151         /// </summary>
    152         /// <param name="areaCode">区域代码</param>
    153         /// <returns>结果</returns>
    154         public static AreaInformation GetArea(string areaCode)
    155         {
    156             Area targetArea = null;
    157             if (!string.IsNullOrWhiteSpace(areaCode) && areaCode.Length == 6)
    158             {
    159                 string provinceCode = areaCode.Substring(0, 2);
    160                 if (areas.ContainsKey(provinceCode))
    161                 {
    162                     var province = areas[provinceCode];
    163                     string cityCode = areaCode.Substring(2, 2);
    164                     if (province.ContainsChild(cityCode))
    165                     {
    166                         var city = province.GetChild(cityCode);
    167                         string countyCode = areaCode.Substring(4);
    168                         if (city.ContainsChild(countyCode))
    169                         {
    170                             targetArea = city.GetChild(countyCode);
    171                         }
    172                         else
    173                         {
    174                             targetArea = city;
    175                         }
    176                     }
    177                     else if (province.ContainsChild(areaCode.Substring(2)))
    178                     {
    179                         targetArea = province.GetChild(areaCode.Substring(2));
    180                     }
    181                     else
    182                     {
    183                         targetArea = province;
    184                     }
    185                 }
    186             }
    187             return targetArea == null ? null : targetArea.ToAreaInformation();
    188         }
    189     }
    190 
    191     /// <summary>
    192     /// 区域
    193     /// </summary>
    194     public class Area
    195     {
    196         /// <summary>
    197         /// 子区域
    198         /// </summary>
    199         private readonly Dictionary<string, Area> childrenDic;
    200 
    201         /// <summary>
    202         /// 区域集
    203         /// </summary>
    204         private readonly List<Area> childrenList;
    205 
    206         /// <summary>
    207         /// Initializes a new instance of the <see cref="Area"/> class. 
    208         /// </summary>
    209         /// <param name="code">代码</param>
    210         /// <param name="name">名称</param>
    211         /// <param name="parent">父区域</param>
    212         internal Area(string code, string name, Area parent)
    213         {
    214             this.Info = new CodeNameMapping(code, name);
    215             this.Parent = parent;
    216             this.childrenDic = new Dictionary<string, Area>();
    217             this.childrenList = new List<Area>();
    218         }
    219 
    220         /// <summary>
    221         /// 代码名称映射信息
    222         /// </summary>
    223         public CodeNameMapping Info
    224         {
    225             get;
    226             private set;
    227         }
    228 
    229         /// <summary>
    230         /// 父区域
    231         /// </summary>
    232         public Area Parent
    233         {
    234             get;
    235             private set;
    236         }
    237 
    238         /// <summary>
    239         /// 子区域
    240         /// </summary>
    241         public ReadOnlyCollection<Area> Children
    242         {
    243             get
    244             {
    245                 return this.childrenList.AsReadOnly();
    246             }
    247         }
    248 
    249         /// <summary>
    250         /// 区域集是否包含
    251         /// </summary>
    252         /// <param name="code">代码</param>
    253         /// <returns>结果</returns>
    254         internal bool ContainsChild(string code)
    255         {
    256             return this.childrenDic.ContainsKey(code);
    257         }
    258 
    259         /// <summary>
    260         /// 获取区域
    261         /// </summary>
    262         /// <param name="code">代码</param>
    263         /// <returns>区域</returns>
    264         internal Area GetChild(string code)
    265         {
    266             return this.childrenDic[code];
    267         }
    268 
    269         /// <summary>
    270         /// 父亲区域
    271         /// </summary>
    272         internal Area TopParent
    273         {
    274             get
    275             {
    276                 return this.Parent == null ? this : this.Parent.TopParent;
    277             }
    278         }
    279 
    280         /// <summary>
    281         /// 添加子区域
    282         /// </summary>
    283         /// <param name="child">子节点</param>
    284         internal void AppendChild(Area child)
    285         {
    286             if (!this.childrenDic.ContainsKey(child.Info.Code))
    287             {
    288                 this.childrenDic.Add(child.Info.Code, child);
    289                 this.childrenList.Add(child);
    290             }
    291         }
    292 
    293         /// <summary>
    294         /// 区域信息转化
    295         /// </summary>
    296         /// <returns>区域信息</returns>
    297         internal AreaInformation ToAreaInformation()
    298         {
    299             CodeNameMapping province = this.TopParent.Info;
    300             CodeNameMapping city = default(CodeNameMapping);
    301             CodeNameMapping county = default(CodeNameMapping);
    302             if (this.Parent != null)
    303             {
    304                 if (this.Parent.Info == province)
    305                 {
    306                     city = this.Info;
    307                 }
    308                 else
    309                 {
    310                     city = this.Parent.Info;
    311                     county = this.Info;
    312                 }
    313             }
    314             return new AreaInformation(province, city, county);
    315         }
    316     }
    317 
    318     /// <summary>
    319     /// 区域信息
    320     /// </summary>
    321     public class AreaInformation
    322     {
    323         /// <summary>
    324         /// Initializes a new instance of the <see cref="AreaInformation"/> class. 
    325         /// </summary>
    326         /// <param name="province"></param>
    327         /// <param name="city"></param>
    328         /// <param name="county"></param>
    329         public AreaInformation(CodeNameMapping province, CodeNameMapping city, CodeNameMapping county)
    330         {
    331             this.Province = province;
    332             this.City = city;
    333             this.County = county;
    334         }
    335 
    336         /// <summary>
    337         /// 代码
    338         /// </summary>
    339         public string Code
    340         {
    341             get
    342             {
    343                 return this.Province.Code + this.City.Code + this.County.Code;
    344             }
    345         }
    346 
    347         /// <summary>
    348         ///349         /// </summary>
    350         public CodeNameMapping Province
    351         {
    352             get;
    353             private set;
    354         }
    355 
    356         /// <summary>
    357         ///358         /// </summary>
    359         public CodeNameMapping City
    360         {
    361             get;
    362             private set;
    363         }
    364 
    365         /// <summary>
    366         ///367         /// </summary>
    368         public CodeNameMapping County
    369         {
    370             get;
    371             private set;
    372         }
    373 
    374         /// <summary>
    375         /// 名称
    376         /// </summary>
    377         public string FullName
    378         {
    379             get
    380             {
    381                 return this.Province.Name + this.City.Name + this.County.Name;
    382             }
    383         }
    384 
    385         /// <summary>
    386         /// 重写ToString
    387         /// </summary>
    388         /// <returns>结果</returns>
    389         public override string ToString()
    390         {
    391             return this.FullName;
    392         }
    393     }
    394 
    395     /// <summary>
    396     /// 代码名称映射
    397     /// </summary>
    398     public struct CodeNameMapping
    399     {
    400         /// <summary>
    401         /// 代码
    402         /// </summary>
    403         private readonly string code;
    404 
    405         /// <summary>
    406         /// 名称
    407         /// </summary>
    408         private readonly string name;
    409 
    410         /// <summary>
    411         /// Initializes a new instance of the <see cref="CodeNameMapping"/> struct. 
    412         /// </summary>
    413         /// <param name="code">代码</param>
    414         /// <param name="name">名称</param>
    415         internal CodeNameMapping(string code, string name)
    416         {
    417             this.code = code;
    418             this.name = name;
    419         }
    420 
    421         /// <summary>
    422         /// 代码
    423         /// </summary>
    424         public string Code
    425         {
    426             get { return this.code; }
    427         }
    428 
    429         /// <summary>
    430         /// 名称
    431         /// </summary>
    432         public string Name
    433         {
    434             get { return this.name; }
    435         }
    436 
    437         /// <summary>
    438         /// 重写比较
    439         /// </summary>
    440         /// <param name="obj">对象</param>
    441         /// <returns>结果</returns>
    442         public override bool Equals(object obj)
    443         {
    444             if (obj != null && obj is CodeNameMapping)
    445             {
    446                 return ((CodeNameMapping)obj).Code == this.Code;
    447             }
    448             return false;
    449         }
    450 
    451         /// <summary>
    452         /// GetHashCode
    453         /// </summary>
    454         /// <returns>HashCode</returns>
    455         public override int GetHashCode()
    456         {
    457             return this.Code.GetHashCode();
    458         }
    459 
    460         /// <summary>
    461         /// 相等比较器
    462         /// </summary>
    463         /// <param name="left">left</param>
    464         /// <param name="right">right</param>
    465         /// <returns>结果</returns>
    466         public static bool operator ==(CodeNameMapping left, CodeNameMapping right)
    467         {
    468             return left.Code != right.Code;
    469         }
    470 
    471         /// <summary>
    472         /// 不相等比较器
    473         /// </summary>
    474         /// <param name="left">left</param>
    475         /// <param name="right">right</param>
    476         /// <returns>结果</returns>
    477         public static bool operator !=(CodeNameMapping left, CodeNameMapping right)
    478         {
    479             return left.Code != right.Code;
    480         }
    481     }
    482 }
    View Code

    xml配置文件

    下载地址

    测试方法

     1         private static void Validator()
     2         {
     3             // 正确的
     4             Validator v = new Validator("330719196804253671");
     5             var rel = v.Execute();
     6 
     7             // 错误的 更改第5位数
     8             Validator v1 = new Validator("330729196804253671");
     9             var rel1 = v1.Execute();
    10         }
    View Code

    js验证点这里

  • 相关阅读:
    JXOI2018简要题解
    TJOI2018简要题解
    BJOI2018简要题解
    【题解】Luogu P4091 [HEOI2016/TJOI2016]求和
    【题解】Luogu P5301 [GXOI/GZOI2019]宝牌一大堆
    【题解】Luogu P5291 [十二省联考2019]希望
    【题解】Luogu P3349 [ZJOI2016]小星星
    【题解】Luogu P5327 [ZJOI2019]语言
    【题解】Luogu P5319 [BJOI2019]奥术神杖
    【题解】Luogu P5471 [NOI2019]弹跳
  • 原文地址:https://www.cnblogs.com/liuxiaoji/p/4533763.html
Copyright © 2020-2023  润新知