• 身份证号码


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

     身份证验证算法

    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)
            {
    
            }
    
        }
    }
    

     

  • 相关阅读:
    hibernate缓存清除(转)
    hibernate缓存
    hibernate延迟加载
    session进行增删改查操作
    curl命令详解
    CURL 宏定义列表
    CURL常用命令---样例
    打印 上一主题 下一主题 利用cURL实现单个文件分多段同时下载,支持断点续传(修订版)
    curl断点续载
    CURL常用命令
  • 原文地址:https://www.cnblogs.com/my-cat/p/7266767.html
Copyright © 2020-2023  润新知