验证二代身份证真假

来源:互联网 发布:网络优化课程 编辑:程序博客网 时间:2024/04/28 01:58

看了几天的视频,做了一个小程序,查询你的二代身份证是否是真的

源代码如下:

 

namespace 验证身份证算法
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnview_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.ToString()))
         {
                MessageBox.Show("号码不能为空");
                textBox1.Focus();
                return;
          
         }
            if (textBox1.TextLength !=18 )
            {
                MessageBox.Show("输入的号码长度为18位");
                return;

            }
            int sum = 0;
            string checkwei = "10x98765432";
            string ID = textBox1.Text;
            string number17 = ID.Substring(0, 17);
            string number18 = ID.Substring(17);
            int[] Wquan = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            for (int i = 0; i < 17; i++)
            {
                sum = sum + Wquan[i] * Convert.ToInt32(number17[i].ToString());
            }
            string result = checkwei[sum % 11].ToString();
            if (result.Equals(number18,StringComparison.OrdinalIgnoreCase))
            {
                MessageBox.Show("身份证合法");
                this.pic朱逢娅.Visible = true;
            }
            else
            {
                MessageBox.Show("身份证不合法");
            }
            int 年龄 = Convert.ToInt32(textBox1.Text.Substring(6,4 ));
            int year = DateTime.Now.Year - 年龄;
            if (year <18)
            {
                this.pic朱逢娅.Visible = false;
                MessageBox.Show("年龄太小回家看动画片吧");
            }

        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
            }
            if (e.KeyChar ==8)
            {
                e.Handled = false;
               
            }
      
            //440524188001010014
            //4405,2418,8001,0100,14
           
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            pic朱逢娅.Visible = false;
       
        }