form1.cs

来自「嵌入式开发从基础到提高」· CS 代码 · 共 182 行

CS
182
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace IDChecker
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void txtID_TextChanged(object sender, EventArgs e)
            //这个事件会在txtID文本输入框的内容被改变时执行,目的是为了显示当前已经
            //输入的字符个数。
        {
            lblInfo.Text = "You Have been Input " + txtID.Text.Length + " Strings";
            if (txtID.Text.Length > 18)
            {
                lblInfo.Text = "To much Numbers!!";
            }
        }

        private void btnClean_Click(object sender, EventArgs e)
            //这是“Clean”按钮的单击事件,当这个按钮被单击操作时,便会执行这些个操
            //作,包括:将txtID文本输入框内容清空,并消息提示框的文本内容改为“Clea
            //red”,并将性别选择框中的选择取消。
        {
            txtID.Text = "";
            lblInfo.Text = "Cleared.";
            rbMan.Checked = false;
            rbWoman.Checked = false;
        }

        private void btnCheck_Click(object sender, EventArgs e)
            //这是“Check”按钮的单击事件,当这个按钮被单击操作时,便会执行这些个操
            //作,它的主要作用是进行整个验证。
        {
            string strID = txtID.Text;
            //用来存储用户输入的需要验证的身份证号
            byte byteIsNumorX = 0;
            //一个标志,用于记录用户输入的字符串是否含有非法字符,如果含有则置为0,
            //否则轩为1
            byte byteInputSex = 0;
            //对用户所进行的性别选择进行记载,当用户没有选择性别时,将其置为0,如果
            //选择为男性,则置为1,如果选择为女性,则置为2
            if (txtID.Text.Length >= 1)
            {
                if (rbMan.Checked)
                    //如果用户选择了身份证编码的对象为男性,则置intInputSex为1
                {
                    byteInputSex = 1;
                }
                if (rbWoman.Checked)
                    //如果用户选择了身份证编码的对象为女性,则置intInputSex为0
                {
                    byteInputSex = 2;
                }
                for (int i = 0; i <= txtID.Text.Length - 1; i++)
                    //该循环的主要目的是为了验证用户输入的需要验证的字符身份证号码中
                    //是否包含了非法字符
                {
                    string strIDEachBit = txtID.Text.Substring(i,1);
                    //每次从用户输入的身份证号码中取出一个字符进行合法性验证
                    if ((IdChk.ischarlegal(strIDEachBit) == 1) || (i == 17 && IdChk.ischarlegal(strIDEachBit) == 2))
                        //进行字符合法性判断,除了第18位上的字符,其它字符都不能为非数字
                    {
                        byteIsNumorX = 1;
                        //字符合法,由标志位置1
                    }
                    else
                        //输入的需要验证的身份证号码中含有非法字符或者X出现在了不对
                        //的位置,提示错误,并告诉用户错误的原因
                    {
                        lblInfo.Text = "The X isnot in the Right Place,or other letters in the ID";
                        IdChk.Msg("The ID is illegal!", 1);
                        goto end;
                    }
                }
                if (byteIsNumorX == 1)
                    //如果输入的身份证号码从字符上讲上合法的,则进行下一步的验证
                {
                    switch (txtID.Text.Length)
                            //判断输入的身份证号码的长度,并做不同的处理
                    { 
                        case 15:
                            //如果输入的身份证是15位号,则将其扩充为17位码
                            strID = strID.Substring(0, 6) + "19" + strID.Substring(6);
                            break;
                        case 18:
                            //如果输入的身份证号是18位号,则对它的校验位进行验证,如果验证
                            //通过,则亦将其处理为17位码
                            if (IdChk.is18ChkNumRight(ref strID) == 1)
                                //判断校验位是否正确,如果正确则将其处理为17位码,并进入下
                                //一步的判断
                            {
                                strID = strID.Substring(0,17);
                                //取18位身份证号码的前17位
                            }
                            else
                                //如果错误,则提示用户18位码的校验码错误
                            {
                                lblInfo.Text = "The Last Check Code is Wrong!";
                                IdChk.Msg("The ID is illegal!",1);
                                goto end;
                            }
                            break;
                        default:
                            //如果不是15或者18位码,则提示用户输入的身份证号码长度有误
                            lblInfo.Text = "The length of the ID is not 15 or 18";
                            IdChk.Msg("The length of the ID is illegal!",1);
                            goto end;
                    }
                    if (strID.Length == 17)
                        //首先验证一下,15位或者18位码是否已经被正确的处理为17位码,如果是,则
                        //进行下一步的验证,否则,提示程序在断送的过程中出现了异常,请重新输入
                        //并再一次验证
                    {
                        if (IdChk.is17DateRight(strID) == 1)
                            //判断日期是否合法,如果合法,则进入下一步的判断
                        {
                            if (byteInputSex == 0)
                                //先判断用户是否输入了性别信息,如果没有输入,则提示用户输入的身
                                //份证号码除了性别信息都是正确的,并提示用户选择性别再验证
                            {
                                lblInfo.Text="The ID is legal except the Sex, please select the Sex and Check again.";
                                IdChk.Msg("The ID is Legal except the Sex!",3);
                                goto end;
                            }
                            else
                                //如果用户选择了性别,则进行性别的验证
                            {
                                int intSexTemp = Convert.ToChar(strID.Substring(16,1));
                                //从用户输入的身份证号码中读取性别信息,即取17位码的最后一位
                                if (intSexTemp % 2 == byteInputSex % 2)
                                    //如果二者同为奇数或者同为偶数,则提示用户输入的身份证号码是
                                    //合法的,否则提示用户输入的身份证号码的性别信息有误
                                {
                                    lblInfo.Text = "The ID is legal!";
                                    IdChk.Msg("The ID is Legal!", 3);
                                    goto end;
                                }
                                else
                                {
                                    lblInfo.Text = "The Sex in the ID and Selected isnot matching!";
                                    IdChk.Msg("The ID is illegal!", 1); 
                                    goto end;
                                }
                            }
                        }
                        else
                        {
                            lblInfo.Text = "The Date Information in the ID is illegal";
                            IdChk.Msg("The ID is illegal!",1);
                            goto end;
                        }
                            
                    }
                    else
                    {
                        lblInfo.Text = "Process have some exceptional, please input the ID again!";
                        IdChk.Msg("Exceptional occurred!",1);
                        goto end;
                    }
                }
            }
            else
            { 
                lblInfo.Text = "Please Input the Numbers";
                IdChk.Msg("Please Input the ID!",1);
            }
        end:
            //“Check”按钮单击事件的末尾
            ;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?