zoukankan      html  css  js  c++  java
  • C#获取(大陆)身份证基本信息的算法

    大陆身份证编码规则(传送门):http://wenda.tianya.cn/question/591202b6b237d943

    下列代码中的地区代码表(传送门):http://www.cnblogs.com/ExDevilLee/p/3447734.html


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    /**********获取大陆身份证信息**********************
     * 通过15位或者18位身份证号计算:生日、性别、地区
     **********************************************/
    namespace GetIDCardInfoDemo
    {
        class GetIDCardInfoCls
        {
    
            //地区代码表
            DistrictCodeTable m_DistrictCode = new DistrictCodeTable();
    
            /// <summary>
            /// 分析身份证号,返回:生日、性别、地区(简)、地区(全)
            /// </summary>
            public string[] AnalyzeIDCard(string strIDNum, out bool isOK)
            {
                try
                {
                    string[] retInfo = new string[4];
                    int strLen = strIDNum.Length;//数据长度
    
                    #region 粗过滤:用正则表达式去校验身份证号的输入
                    string rule = string.Empty;
                    if (strLen == 15) rule = @"d{6}d{2}[0-1]d[0-3]d{4}";
                    if (strLen == 18) rule = @"d{6}[1-2]d{3}[0-1]d[0-3]d{4}[0-9X]";
                    if (!rule.Equals(string.Empty))
                    {
                        System.Text.RegularExpressions.Regex regex = new Regex(rule);
                        isOK = regex.IsMatch(strIDNum);
                    }
                    else
                    {
                        isOK = false;
                    }
                    #endregion
    
                    #region 细过滤:判断前两位数字是否为正确的地区代码
                    if (isOK)
                    {
                        string headAB = strIDNum.Substring(0,2);
                        m_DistrictCode.InitDistrictTable_Short();
                        //若前两位数字在地区代码中不存在,则为假
                        if (!m_DistrictCode.m_DistrictTB.Contains(headAB))
                        {
                            isOK = false;
                        }
                    }
                    #endregion
    
                    #region 细过滤:出生日期是否可转换
                    if (isOK)
                    {
                        //更改15位身份证中的日期格式
                        if (strLen == 15) strIDNum = strIDNum.Insert(6, "19");
                        string testBir = strIDNum.Substring(6, 8).Insert(4, "-").Insert(7, "-");
                        DateTime testRes;
                        DateTime.TryParse(testBir, out testRes);
                        //若转换结果为最小值,这说明转换失败
                        if (testRes == DateTime.MinValue)
                        {
                            isOK = false;
                        }
                    }
                    #endregion
    
                    //基础检查完成后的操作
                    if (!isOK)
                    {
                        MessageBox.Show("请输入15位或18位有效身份证号!");
                        return null;
                    }
                    else
                    {
                        retInfo = GetIDCardInfo(strIDNum);
                    }
                    return retInfo;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    throw ex;
                }
            }
    
            /// <summary>
            /// 获取身份证信息
            /// </summary>
            /// <returns>生日、性别、地区(简)、地区(全)</returns>
            private string[] GetIDCardInfo(string IDCardNo)
            {
                try
                {
                    string[] info = new string[4];
                    int sex = -1;
                    //更改15位身份证中的日期格式
                    if (IDCardNo.Length == 15) IDCardNo = IDCardNo.Insert(6, "19");
                    //info[0]==生日
                    info[0] = IDCardNo.Substring(6, 8).Insert(4, "-").Insert(7, "-");
                    //info[1]==性别
                    sex = int.Parse(IDCardNo.Substring(16, 1));
                    info[1] = sex % 2 == 1 ? "" : "";
                    //info[2]==地区(简)
                    info[2] = m_DistrictCode.GetDistrictCode(IDCardNo.Substring(0, 2),(int)DistrictCodeTable.DistrictResultType.Short);
                    //info[3]==地区(全)
                    info[3] = m_DistrictCode.GetDistrictCode(IDCardNo.Substring(0, 2),(int)DistrictCodeTable.DistrictResultType.Full);
                    return info;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    
        }
    }

    小弟才疏学浅,难免会有错误,若您发现了请及时指出,谢谢!

    写此文只为记录编程过程中积攒的思想与经验,若您有更好的思路希望您能给我留言,谢谢啦!~

  • 相关阅读:
    BZOJ 2212/BZOJ 3702
    BZOJ 4761 Cow Navigation
    BZOJ 3209 花神的数论题
    BZOJ 4760 Hoof, Paper, Scissors
    BZOJ 3620 似乎在梦中见过的样子
    BZOJ 3940 Censoring
    BZOJ 3942 Censoring
    BZOJ 3571 画框
    BZOJ 1937 最小生成树
    BZOJ 1058 报表统计
  • 原文地址:https://www.cnblogs.com/ExDevilLee/p/3470680.html
Copyright © 2011-2022 走看看