/// <summary>
/// 身份证
/// </summary>
[Serializable]
public class IDCard
{
    /// <summary>
    /// 身份证号
    /// </summary>
    public string IDCardNum { get; set; }
    /// <summary>
    /// 行政区
    /// </summary>
    public string Canton { get; private set; }
    /// <summary>
    /// 出生日期
    /// </summary>
    public DateTime Birthday { get; private set; }
    /// <summary>
    /// 性别(0-女;1-男)
    /// </summary>
    public int Gander { get; private set; }
    /// <summary>
    /// 是否为合法身份证号
    /// </summary>
    public bool IsIDCard { get; private set; }     public IDCard() { }     public IDCard(string IDnumber)
    {
        this.IDCardNum = IDnumber;
    }     /// <summary>
    /// 
    /// </summary>
    /// <param name="number"></param>
    /// <returns></returns>
    public static IDCard Parse(string number)
    {
        IDCard idCard = new IDCard(number);         const int s5bits = 15;
        const int s8bits = 18;         #region 15位
        if (number.Length == s5bits)  //15位的处理
        {
            //检查输入是否为数字
            for (int i = 0; i < number.Length; i++)
            {
                if ((number[i] < '0') || (number[i] > '9'))
                {
                    throw new FormatException("身份证号错误");
                }
            }             //出生日期
            string birthday = "19" + number.Substring(6, 6);
            string year = birthday.Substring(0, 4);
            string month = birthday.Substring(4, 2);
            string day = birthday.Substring(6, 2);
            birthday = string.Format("{0}-{1}-{2}", year, month, day);             DateTime date = new DateTime();
            if (DateTime.TryParse(birthday, out date))
            {
                idCard.Birthday = date;
            }
            else
            {
                throw new InvalidCastException("身份证号出生日期错误");
            }
            
            //性别
            if ((number[s5bits - 1] == '0') || (number[s5bits - 1] % 2 == 0))
            {
                idCard.Gander = 0; // 女
            }
            else
            {
                idCard.Gander = 1; // 男
            }             idCard.IsIDCard = true;
            return idCard;
        }
        #endregion         #region 18位
        else if (number.Length == s8bits)  //18位的处理
        {
            // 检查前17位是否为数字
            for (int i = 0; i < number.Length -1; i++)
            {
                if ((number[i] < '0') || (number[i] > '9'))
                {
                    throw new FormatException("身份证号错误");
                }
            }             char end = number[s8bits - 1];  //最后一位             //最后1位是x转成大写X
            if (end == 'x')
            {
                end = 'X';
                number = number.Substring(0, s8bits - 1) + end;
            }             if (!(end == 'X' || (end >= '0' && end <= '9')))
            {
                throw new FormatException("身份证号错误");
            }
            
            /// 校验
            int num = 0;
            char proof;
            for (int i = 17; i > 0; i--)
            {
                num = num + (int)(Math.Pow(2, i) % 11) * (number[17 - i] - 48);
            }
            num %= 11;
            switch (num)
            {
                case 0:
                    proof = '1';
                    break;
                case 1:
                    proof = '0';
                    break;
                case 2:
                    proof = 'X';
                    break;
                default:
                    proof = (char)(12 - num + 48);
                    break;
            }             if (end != proof)  //最后一位与校验码不符
            {
                throw new FormatException("身份证号错误");
            }             //出生日期
            string birthday = number.Substring(6, 8);
            string year = birthday.Substring(0, 4);
            string month = birthday.Substring(4, 2);
            string day = birthday.Substring(6, 2);
            birthday = string.Format("{0}-{1}-{2}", year, month, day);             DateTime date = new DateTime();
            if (DateTime.TryParse(birthday, out date))
            {
                idCard.Birthday = date;
            }
            else
            {
                throw new InvalidCastException("身份证号出生日期错误");
            }             //行政区
            idCard.Canton = number.Substring(0, 6);             //性别
            if ((number[16] == '0') || (number[16] % 2 == 0))
            {
                idCard.Gander = 0;  //女
            }
            else
            {
                idCard.Gander = 1;  //男
            }             idCard.IsIDCard = true;
            return idCard;
        }
        #endregion
        else
        {
            throw new FormatException("无效的身份证号码位数:" + number.Length);
        }
    }     public static bool TryParse(string number, out IDCard card)
    {
        IDCard idCard = null;
        bool isIdCard = true;
        try
        {
            Parse(number);
        }
        catch (Exception)
        {
            isIdCard = false;
        }
        card = idCard;
        return isIdCard;
    }
}

最新文章

  1. 自定义属性,资源文件attrs.xml
  2. Oracle 11g EM安全证书问题无法访问的解决办法
  3. Swift实战-QQ在线音乐(AppleWatch版)
  4. c#网络通信框架networkcomms内核解析之八 数据包的核心处理器
  5. studio-引入外来包
  6. VIM配置(转载)
  7. 获取json对象的id或者根据name获取id
  8. ubuntu下firefox安装Adobe Flash Player
  9. HDU 4508 沼泽湿地系列故事——记住减肥I (2013腾讯编程马拉松预赛第一)
  10. HDU---Labyrinth
  11. 模板引擎(smarty)知识点总结三
  12. 项目开发过程中什么是开发环境、测试环境、生产环境、UAT环境、仿真环境?
  13. Java 并发编程-NIO 简明教程
  14. developer的996,需要谁来拯救
  15. WireShark抓包工具使用
  16. 将本地代码同步到远程github上
  17. Git服务器的搭建和使用
  18. Net分布式系统整体框架
  19. OutLook中添加Exchange失败问题
  20. ASK,OOK,FSK,GFSK是什么

热门文章

  1. 利用javascript动态加载头部出现点击事件与hover事件无效解决方法
  2. foreachRDD
  3. windows下git安装过程
  4. 桌面图标管理工具-Rolan(网上收集,仅供学习与研究,支持正版)
  5. Shell编程——位置参数变量
  6. appium--Toast元素识别
  7. CSP2019题解
  8. [LeetCode] 894. All Possible Full Binary Trees 所有可能的满二叉树
  9. Spring Cloud Alibaba整合Sentinel流控
  10. java语言规范