码迷,mamicode.com
首页 > 其他好文 > 详细

转载 根据身份证号得到性别跟出生日期

时间:2014-05-21 05:34:00      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:style   c   tar   ext   color   a   

/// <summary>
        /// 在控件验证 textBox_IdentityCard 的 Validated事件中定义身份证号码的合法性并根据身份证号码得到生日和性别
        /// </summary>
        private void textBox_IdentityCard_Validated(object sender, EventArgs e)
        {
            try
            {
                string identityCard = textBox_IdentityCard.Text.Trim();//获取得到输入的身份证号码

                if (string.IsNullOrEmpty(identityCard))
                {
                    MessageBox.Show("身份证号码不能为空!");//身份证号码不能为空,如果为空返回
                    if (textBox_IdentityCard.CanFocus)
                    {
                        textBox_IdentityCard.Focus();//设置当前输入焦点为textBox_IdentityCard
                    }
                    return;
                }
                else
                {
                    if (identityCard.Length != 15 && identityCard.Length != 18)//身份证号码只能为15位或18位其它不合法
                    {
                        MessageBox.Show("身份证号码为15位或18位,请检查!");
                        if (textBox_IdentityCard.CanFocus)
                        {
                            textBox_IdentityCard.Focus();
                        }
                        return;
                    }
                }
                string birthday = "";
                string sex = "";
                if (identityCard.Length == 18)//处理18位的身份证号码从号码中得到生日和性别代码
                {
                    birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2);
                    sex = identityCard.Substring(14, 3);
                }
                if (identityCard.Length == 15)
                {
                    birthday = "19" + identityCard.Substring(6, 2) + "-" + identityCard.Substring(8, 2) + "-" + identityCard.Substring(10, 2);
                    sex = identityCard.Substring(12, 3);
                }
                textBox_Birthday.Text = birthday;
                if (int.Parse(sex) % 2 == 0)//性别代码为偶数是女性奇数为男性
                {
                    this.comboBox_Sex.Text = "女";
                }
                else
                {
                    this.comboBox_Sex.Text = "男";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("身份证号码输入有误");
                if (textBox_IdentityCard.CanFocus)
                {
                    textBox_IdentityCard.Focus();
                }
                return;
            }
        }

转载 根据身份证号得到性别跟出生日期,布布扣,bubuko.com

转载 根据身份证号得到性别跟出生日期

标签:style   c   tar   ext   color   a   

原文地址:http://www.cnblogs.com/miraclesakura/p/3739356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!