身份证校验

本文介绍了一个用于验证和解析中国身份证号码的C#类。该类可以检查15位及18位身份证号码的有效性,并从中提取年龄和性别信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise
{
    public static class CIDCard        ///判断身份证号是否合法///
    {
       private static bool bFlag = false;
       public static  string Sex;
       public static  int Age;
       public static Boolean CheckId(String idNumber)
        {
            string strId = idNumber;
            int idNum = strId.Length;
            switch (idNum)
            {
                case 15:   //判断是否为15位身份证号
                    bFlag = ckID(strId);
                    return true;
                case 18:
                    bFlag = ckID18(strId);   //判断是否为18位身份证号
                    return true;
                default:             //缺省为身份证号不合法
                   return false;
            }

        }
        public static Boolean ckID(string strID15)
        {
            string sex0;
            string str15 = strID15;
            //DateTime date = new DateTime();
            int currentYear = DateTime.Now.Year;        //存放当前年份
            int IdYear = 1900 + Convert.ToInt16(str15.Substring(6, 2));
            int Age0 = currentYear - IdYear;
            if (Age0 < 16 || Age0 > 60)                    //身份证上年龄不合法
            { return false; }
            else
            {
                Age = Age0;
                int endId = Convert.ToInt16(str15.Substring(14, 1));
                if (endId % 2 == 0)
                {
                    sex0  = "女";
                    Sex = sex0;
                }
                else { sex0 = "男";Sex=sex0 ; }
                return true;
            }
        }
        public static Boolean ckID18(string strID18)
        {
            string Sex1;
            int age1;
            string str18 = strID18;
            int currentYear = DateTime.Now.Year;        //存放当前年份
            int IdYear = Convert.ToInt16(str18.Substring(6, 4));
            age1  = currentYear - IdYear;
            if (age1  < 16 ||age1  > 60)                    //身份证上年龄不合法
            { return false; }
            else
            {
                Age = age1;
                int endId = Convert.ToInt16(str18.Substring(16, 1));
                if (endId % 2 == 0)
                { Sex1 = "女"; Sex = Sex1; }
                else { Sex1 = "男";Sex = Sex1; }
                char idCode = Convert.ToChar(str18.Substring(17, 1));
                if (idCode == Shen(str18))
                {
                    //System.Windows.Forms.MessageBox.Show("客户所持为新身份证,客户年龄为" + Age + ",性别为:" + Sex);
                    return true;
                }
                else { return false; }

            }
        }
        public static char Shen(string id)
        {
            int[] w = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
            char[] a = new char[] { '1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2' };
            int s = 0;
            for (int i = 0; i < 17; i++)
            {
                int k = (Convert.ToInt32(id[i]) - 48) * w[i];
                s = s + k;
            }
            int h = 0;
            Math.DivRem(s, 11, out h);
            char endCode = a[h];
            return endCode;
        }
        public static string getSex()
        {
            return Sex ;
        }
        public static int getAge()
        {
            return Age;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值