zoukankan      html  css  js  c++  java
  • 身份证号码

    偶尔发现身份证号码的规定,觉得甚是好玩,特此标记

     身份证验证算法

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 身份验证算法
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if(e.KeyChar < '0' || e.KeyChar >'9')
                {
                    e.Handled = true;
                }
    
                if((textBox1.SelectionStart == 17) && (e.KeyChar == 'x'||e.KeyChar == 'X'))
                {
                    e.Handled = false;
                }
                
                if(e.KeyChar == 8)
                {
                    e.Handled = false;
                }
    
            }
    
            /// <summary>
            /// 校验身份证信息,如果正确,则返回true,否则false
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            private bool CheckID(string id)
            {
                //身份证1前17位对应的权值
                int[] w = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
                string checkW = "10X98765432";
                string num17 = id.Substring(0, 17);
                string num18 = id.Substring(17);
    
                int sum = 0;
                for (int i = 0 ; i < 17; i++)
                {
                    sum += w[i] * Convert.ToInt32(num17[i].ToString());
                }
    
                int mod = sum % 11;
                string result = checkW[mod].ToString();
    
                //string类型,忽略大小写的比较
                return num18.Equals(result, StringComparison.OrdinalIgnoreCase);
    
            }
            private void button1_Click(object sender, EventArgs e)
            {
                int age = 0;
                int year = 0;
                //15位:省(2)市(2)区县(2)年(2)月(2)日(2)+3个序列号[奇数男/偶数女]
                //第7、8位
                string id = textBox1.Text.Trim();//去掉两边的空格
                if(id.Length == 15)
                {
                    year = Convert.ToInt32(id.Substring(6,2))+1900;
                    //age = DateTime.Now.Year - year;
                }
                //18位:年前面一般都加上19 第18位为校验位,从前面17位计算而来
                else if(id.Length == 18)
                {
                    if(!this.CheckID(id))
                    {
                        MessageBox.Show("身份证号码有误");
                        return;
                    }
    
                    year = Convert.ToInt32(id.Substring(6, 4));
                    //age = DateTime.Now.Year - year;
                }
                else
                {
                    MessageBox.Show("长度有误");
                    return;
                }
    
                age = DateTime.Now.Year - year;
                if(age >= 18)
                {
                    pictureBox1.Visible = true;
                }
                else
                {
                    MessageBox.Show("nothing");
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
        }
    }
    

     

  • 相关阅读:
    SCU 3133(博弈)
    SCU 3132(博弈)
    hdu 5183(hash)
    hdu3329(2次dfs)
    hdu5179(数位dp)
    zoj2314(有上下界的网络流)
    CF 519E(树上倍增求lca)
    hdu1251(Trie树)
    SCU 2009(数位dp)
    【Leetcode】Letter Combinations of a Phone Number
  • 原文地址:https://www.cnblogs.com/my-cat/p/7266767.html
Copyright © 2011-2022 走看看