/// <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;
    }
}

C# 身份证号码15位和18位验证的更多相关文章

  1. 做一个牛XX的身份证号验证类(支持15位和18位)

    原文:做一个牛XX的身份证号验证类(支持15位和18位) #region 是否合法的中国身份证号码 protected bool IsChineseID() { if (str.Length == 1 ...

  2. java中身份证号15位转18位

    /** * 将15位转换为18位 * @param idCode 15位身份证号 * @return String 18位身份证号 */ public String toEighteen(String ...

  3. php提取身份证号码中的生日日期以及验证是否为未成年人的函数

    php 提取身份证号码中的生日日期以及确定是否成年的一个函数.可以同时确定15位和18位的身份证,经本人亲测,非常好用,分享函数代码如下: <?php //用php从身份证中提取生日,包括15位 ...

  4. 身份证号码15位转18位 C#实现

    [身份证最后一位神秘X的由来]身份证中的“冷知识”1999年的今天,<国务院关于实行公民身份号码制度的决定>被发布,当年10月1日实施.为什么有的有X?这位数是根据前17位计算出的校验码. ...

  5. java 身份证15位转18位

    /** * 根据身份证号获取性别 * * @param pid * 身份证号 * @return 性别 F为女M为男 */ public static String getSexByPid(Strin ...

  6. php 验证身份证有效性,根据国家标准GB 11643-1999 15位和18位通用

    //验证身份证是否有效 function validateIDCard($IDCard) { if (strlen($IDCard) == 18) { return check18IDCard($ID ...

  7. 16Aspx.com-将15位身份证转换成18位

    //********************************************************************************* //将15位身份证转换成18位时 ...

  8. Js完美验证15/18身份证,Js验证身份证,支持15/18位

    Js完美验证15/18身份证,Js验证身份证,支持15/18位 >>>>>>>>>>>>>>>>> ...

  9. js验证15位或18位身份证

    本篇文章是本人在网上搜集了一些验证,然后又个人进行一定修改的关于身份证的验证,欢迎修改指正..... function IdCardValidateRule(idCard) { var tip;    ...

随机推荐

  1. vue全家桶项目应用断断续续的记录

    一.引用axios插件报错 axios使用文档 Cannot read property 'protocol' of undefined 解决方法:在mainjs文件中把axios引入vue的原型函数 ...

  2. Spark应用程序开发流程

    配置文件: pom.xml <properties> <scala.version>2.11.8</scala.version> <spark.version ...

  3. emacs cedet

    用emacs写c或者c++代码用的插件的配置.功能是能够代码补齐. (require 'package) (package-initialize) (add-to-list'package-archi ...

  4. Python从零开始——元组tuple

    一:元组知识内容 二:元组的不可变性 三:元组创建 四:元组操作

  5. SQLAlchemy 应用创建

    1.首先创建app文件夹 同django 创建app 一样 创建文件 在创建的views中写入两个蓝图函数为了操作数据库的增删改查 acc.py from flask import Blueprint ...

  6. HDFS 分布式文件系统

    博客出处W3c:https://www.w3cschool.cn/hadoop/xvmi1hd6.html 简介 Hadoop Distributed File System,分布式文件系统 架构 B ...

  7. opencv旋转图像,90度标准旋转

    摘自opencv 源代码 void rotate(InputArray _src, OutputArray _dst, int rotateMode) { CV_Assert(_src.dims() ...

  8. Maven 本地仓库同步到私服中

    步骤: 第一步:找到安装私服的目录中plexus.properties文件. 地址:C:\Windows\apache-tomcat-7.0.26\webapps\nexus-2.7.0-06\WEB ...

  9. streamsets 源码构建

    依赖构建工具 git 1.9+ oracle jdk  8 docker 1.10+ maven  3.3.9+ nodejs npm grunt-cli md5sum 预备构建任务 data col ...

  10. <Stack> 150 71 388

    150. Evaluate Reverse Polish Notation class Solution { public int evalRPN(String[] tokens) { Stack&l ...