身份证号验证

1.XML

<TextBox Grid.Column="1" Grid.Row="5" x:Name="txt_idCar" MaxLength="18" TextChanged="txt_idCar_TextChanged"/>

2.逻辑加载层

namespace _1702_Client.Views.SystemInformation
{
    /// <summary>
    /// WD_InsertStaffInformation.xaml 的交互逻辑
    /// </summary>
    public partial class WD_InsertStaffInformation : Window
    {
        public WD_InsertStaffInformation()
        {
            InitializeComponent();
        }
private void txt_idCar_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            try
            {
                string strIdCard = txt_idCar.Text.Trim();
                //(1)验证身份证准确性
                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_idCar.Text = "";                        
                    }                  
                    else
                    {
                        //(2)切割字符串()
                        string keys = strIdCard;
                        //性别
                        int sex = int.Parse(keys.Substring(16, 1));
                        //取余
                        if (sex % 2 == 0)
                        {                            
                            cbo_gender.SelectedValue = 77;//77跟下拉框ID值对应
                        } 
                        else
                        {                            
                            cbo_gender.SelectedValue = 76;//76跟下拉框ID值对应
                        }

                        //年
                        string birth_y = keys.Substring(6, 4);
                        //月
                        string birth_m = keys.Substring(10, 2);
                        //日
                        string birth_d = keys.Substring(12, 2);
                        //绑定出生日期
                        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();
                    }
                }
                else if (txt_idCar.Text.ToString().Length == 6)
                {                   
                    string strAddress = CheckIDCardGetDiQu.LoadAddress(txt_idCar.Text.ToString());
                    if (strAddress == "")
                    {
                        MessageBox.Show("身份证不合法!");
                    }
                    else
                    {
                        //联系地址
                        txt_Address.Text = strAddress;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }          
上一篇:jQuery中的表单验证


下一篇:Python | Python_条件判断