No 118 · C#根据身份证号码算出性别和年龄

 
#region  身份证验证

        static int[] weight = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        static char[] vCode = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
        string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
        int age = 0;
        bool valid;


        public bool Reg_IdentityCard(string num)
        {
            if (num.Length == 18)
            {
                if (!CheckValidCode(num.Substring(0, 17), num[17]))
                    valid = false;
                else if (!CheckACode(num.Substring(0, 2)))
                    valid = false;
                else if (!GetAge(num.Substring(6, 4)))
                    valid = false;
                else
                    valid = true;
                if (valid)
                {
                    int g = Convert.ToInt32(num.Substring(14, 3));
                    if (g % 2 == 0)
                        this.txtSex.Text = "女";
                    else
                        this.txtSex.Text = "男";
                    this.txtAge.Text = age.ToString();
                    return true;
                }
                else
                {
                    MessageBox.Show("身份证号不正确", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    return false;
                }
            }
            else if (num.Length == 15)
            {
                if (!CheckACode(num.Substring(0, 2)))
                    valid = false;
                else if (!GetAge("19" + num.Substring(6, 2)))
                    valid = false;
                else
                    valid = true;
                if (valid)
                {
                    int g = Convert.ToInt32(num.Substring(12, 3));
                    if (g % 2 == 0)
                        this.txtSex.Text = "女";
                    else
                        this.txtSex.Text = "男";
                    this.txtAge.Text = age.ToString();
                    return true;
                }
                else
                {
                    MessageBox.Show("身份证号不正确", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    return false;
                }
            }
            else
            {
                MessageBox.Show("身份证号不正确", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return false;
            }
        }


        bool CheckValidCode(string input17, char last)//检查最后一位校验码
        {
            if (last == CheckValidCode(input17))
                return true;
            return false;
        }
        char CheckValidCode(string input17)//检查最后一位校验码
        {
            int sum = 0, cur;
            for (int i = 0; i < 17; i++)
            {
                cur = Convert.ToInt32(input17[i]) - 48;
                sum += cur * weight[i];
            }
            return vCode[(sum % 11)];
        }
        bool GetAge(string input4)//算出年龄
        {
            try
            {
                age = Convert.ToInt32(DateTime.Now.Year) - Convert.ToInt32(input4);
                return true;
            }
            catch { return false; }
        }
        bool CheckACode(string input2)//检查地区码
        {
            if (address.IndexOf(input2) != -1)
                return true;
            return false;
        }
        #endregion

你可能感兴趣的:(String,C#,input)