zoukankan      html  css  js  c++  java
  • 国民身份证号码校验之“C#/Winform方法实现+案例分析”

    根据〖中华人民共和国国家标准 GB 11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。地址码表示编码对象常住户口所在县(市、旗、区)的行政区划代码。生日期码表示编码对象出生的年、月、日,其中年份用四位数字表示,年、月、日之间不用分隔符。顺序码表示同一地址码所标识的区域范围内,对同年、月、日出生的人员编定的顺序号。顺序码的奇数分给男性,偶数分给女性。校验码是根据前面十七位数字码,按照ISO 7064:1983.MOD 11-2校验码计算出来的检验码。下面举例说明该计算方法。

    15位的身份证编码首先把出生年扩展为4位,简单的就是增加一个19,但是这对于1900年出生的人不使用(这样的寿星不多了)

    某男性公民身份号码本体码为34052419800101001,首先按照公式⑴计算:

    ∑(ai×Wi)(mod 11)……………………………………(1)

    公式(1)中:

    i----表示号码字符从右至左包括校验码在内的位置序号;

    ai----表示第i位置上的号码字符值;

    Wi----示第i位置上的加权因子,其数值依据公式Wi=2(n-1)(mod 11)计算得出。

    i      18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

    ai     3 4 0   5 2 4 1 9 8   0 0 1 0 1 0 0 1 a1

    Wi     7 9 10 5 8 4 2 1 6   3 7 9 10 5 8 4 2 1

    ai×Wi 21 36 0   25 16 16 2 9 48 0 0 9 0 5 0 0 2 a1

    根据公式(1)进行计算:

    ∑(ai×Wi) =(21+36+0+25+16+16+2+9+48++0+0+9+0+5+0+0+2) = 189

    189 ÷ 11 = 17 + 2/11

    ∑(ai×Wi)(mod 11) = 2

    然后根据计算的结果,从下面的表中查出相应的校验码,其中X表示计算结果为10:

    ∑(ai×WI)(mod 11) 0 1 2 3 4 5 6 7 8 9 10

    校验码字符值ai 1 0 X 9 8 7 6 5 4 3 2

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

    namespace 测试四

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            private void btnLook_Click(object sender, EventArgs e)

            {

                //身份证目前可分为15位和18位

                //15位号码组成:省(2)市(2)区/县(2)年(2)月(2)日(2)+序列号(3)[奇数分配给男性偶数分配给女性]

                //15位升级到18位:出生年都加上19,第二点:第十八位为校验位,也就是说第十八位要从前17位计算而来

                //如果是15位数字,那么我们就取得17,8位时出生年

                //如果是18位,则先校验用户输入的身份证是否合法,如果合法者去7-10位为出生年

                string id = txtId.Text.Trim();

                int age = 0;

                if (id.Length == 15)

                {

                    int year=Convert.ToInt32(id.Substring(6,2))+1900;

                    age = DateTime.Now.Year - year;

                    if (age >= 18)

                    {

                        pictureBox1.Visible = true;

                    }

                    else

                    {

                        MessageBox.Show("年龄太小,不可查看!");

                    }

                }

                else if (id.Length == 18)

                {

                    if (!this.checCardId(id))

     

                    {

                        MessageBox.Show("身份证号码输入有误,请检查!");

                        return;

                    }

                    int year = Convert.ToInt32(id.Substring(6,4));

                    age = DateTime.Now.Year - year;

                    if (age >= 18)

                    {

                        pictureBox1.Visible = true;

                    }

                    else

                    {

                        MessageBox.Show("年龄太小,不可查看!");

                    }

                }

                else

                {

                    MessageBox.Show("您输入的身份证号码长度输入有误!");

                    return;

                }

            }

            /// <summary>

            /// 校验身份证号,如果正确返回true如果错误返回false

            /// </summary>

            /// <param name="id">

            /// //身份证目前可分为15位和18位

            //15位号码组成:省(2)市(2)区/县(2)年(2)月(2)日(2)+序列号(3)[奇数分配给男性偶数分配给女性]

            //15位升级到18位:出生年都加上19,第二点:第十八位为校验位,也就是说第十八位要从前17位计算而来

            //如果是15位数字,那么我们就取得17,8位时出生年

            //如果是18位,则先校验用户输入的身份证是否合法,如果合法者去7-10位为出生年

            /// </param>

            /// <returns></returns>

            private bool checCardId(string id)

            {

                int[] wQuan = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};

                string checkWei = "10X98765432";

                string num17 = id.Substring(0,17);

                string num18 = id.Substring(17);

                int sum = 0;

                for (int i = 0; i < 17; i++)

                {

                    sum += Convert.ToInt32(num17[i].ToString())*wQuan[i];

                }

                int mod = sum % 11;

                string result=checkWei[mod].ToString();

                if (num18.Equals(result, StringComparison.OrdinalIgnoreCase))

                {

                    return true;

                }

                else

                {

                    return false;

                }

     

            }

     

            private void txtId_KeyPress(object sender, KeyPressEventArgs e)

            {

                if(e.KeyChar<'0'||e.KeyChar>'9')

                {

                    //输入数字以外的字符就要阻止调

                    e.Handled=true;

                }

                //如果第十八位输入的是X这不阻止

                if ((txtId.SelectionStart == 17) && (e.KeyChar == 'X' || e.KeyChar == 'x'))

                {

                    e.Handled = false;

                }

                //不阻止backspace键

                if(e.KeyChar==8)

                {

                    e.Handled = false;

                }

            }

     

            private void pictureBox1_Click(object sender, EventArgs e)

            {

     

            }

     

            private void txtId_TextChanged(object sender, EventArgs e)

            {

                pictureBox1.Visible = false;

            }

        }

    }

  • 相关阅读:
    HDU 3401 Trade
    POJ 1151 Atlantis
    HDU 3415 Max Sum of MaxKsubsequence
    HDU 4234 Moving Points
    HDU 4258 Covered Walkway
    HDU 4391 Paint The Wall
    HDU 1199 Color the Ball
    HDU 4374 One hundred layer
    HDU 3507 Print Article
    GCC特性之__init修饰解析 kasalyn的专栏 博客频道 CSDN.NET
  • 原文地址:https://www.cnblogs.com/Jasxu/p/cardid.html
Copyright © 2011-2022 走看看