wpf后台服务源码《身份证号验证》

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;
            }
        }          
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值