其他分享
首页 > 其他分享> > 身份证号码验证

身份证号码验证

作者:互联网

第一步:搭建页面
在这里插入图片描述

页面代码:需要一个文本框改变事件
在这里插入图片描述

第二步:跳转到后台,写后台代码
private void txt_CertificateType_TextChanged(object sender, TextChangedEventArgs e)
{
string strIdCard = txt_CertificateType.Text.Trim();
#region 获取地址
if (txt_CertificateType.Text.ToString().Length == 6)
{
//这里的CheckIDCardGetDiQu需要创建类
string strAddress = CheckIDCardGetDiQu.LoadAddress(txt_CertificateType.Text.ToString());
if (strAddress == “”)
{
MessageBox.Show(“身份证不合法!”);
}
else
{
txt_address.Text = strAddress;
}
}
if (txt_CertificateType.Text.ToString().Length < 6)
{
txt_address.Text = “”;
}
#endregion
#region 验证身份证
try
{
if (strIdCard.Length == 18)
{

                //闰年出生日期的合法性正则表达式 || 平年出生日期的合法性正则表达式 
                if (!Regex.IsMatch(strIdCard, @"(^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$)") || !Regex.IsMatch(strIdCard, @"(^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$)"))
                {
                    MessageBox.Show("身份证不合法!");
                    txt_CertificateType.Text = "";
                }
                else
                {
                    string keys = strIdCard;
                    获取地址码
                    //string dmzm = keys.Substring(0, 6);
                    //性别
                    int sex = int.Parse(keys.Substring(16, 1));
                    //年
                    string birth_y = keys.Substring(6, 4);
                    //月
                    string birth_m = keys.Substring(10, 2);
                    //日
                    string birth_d = keys.Substring(12, 2);
                    ListViewItem l = new ListViewItem();
                    //绑定出生日期
                    dtp_Birthday.Text = birth_y + "年" + birth_m + "月" + birth_d + "日";
                    //获取今年年份
                    string strNow = DateTime.Now.Year.ToString();
                    //把今年转化成数字
                    decimal decNow = Convert.ToDecimal(strNow);
                    //获取(截取身份证)出生年份
                    decimal decbirth_y = Convert.ToDecimal(birth_y);
                    //获取虚岁
                    decimal decAge = Convert.ToDecimal(decNow - decbirth_y) + 1;
                    //绑定年龄
                    txt_Age.Text = decAge.ToString().Trim();
                    //取余
                    if (sex % 2 == 0)
                    {
                        //l.SubItems.Add("女");
                        cbo_Gender.SelectedValue = 77;//77跟下拉框ID值对应
                    } //if
                    else
                    {
                        //l.SubItems.Add("男");
                        cbo_Gender.SelectedValue = 76;//76跟下拉框ID值对应
                    }
                }
            }
        }
        catch
        {
            MessageBox.Show("输入的身份证不正确。");
        }
        #endregion
    }

}

标签:string,号码,验证,keys,Text,birth,身份证,CertificateType,txt
来源: https://blog.csdn.net/sdlfkjsldf/article/details/122185143