dev控件TextEdit的mask设置结合控件的Validated事件来控制身份证录入正确并从中取值生成性别、生日

55 篇文章 1 订阅
5 篇文章 0 订阅

1、dev控件TextEdit的mask设置


2、控件的Validated事件

    private void S_9_Validated(object sender, EventArgs e)
        {
            try
            {
                string identityCard = S_9.Text.Trim();//获取得到输入的身份证号码                
                 if (string.IsNullOrEmpty(identityCard))
                {
                    MessageBox.Show("身份证号码不能为空!");//身份证号码不能为空,如果为空返回
                    if (S_9.CanFocus)
                    {
                        S_9.Focus();//设置当前输入焦点为textBox_IdentityCard
                    }
                    return;
                }
                 if (Convert.ToInt32(DataClass.MyMeans.ExecuteScalar(@"select count(*) from tb_Stuffbusic where IDCard = @idcard and Employee in ('正式工','试用工')", CommandType.Text, new SqlParameter[] { new SqlParameter("@idcard", identityCard) })) > 1)
                 {
                     MessageBox.Show("身份证号码重复,已占用,请检查录入!");
                     S_9.Focus();
                 }
                else
                {
                    if (identityCard.Length != 15 && identityCard.Length != 18)//身份证号码只能为15位或18位其它不合法
                    {
                        MessageBox.Show("身份证号码为15位或18位,请检查!");
                        if (S_9.CanFocus)
                        {
                            S_9.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);
                }
                S_3.Text = birthday;
                if (int.Parse(sex) % 2 == 0)//性别代码为偶数是女性奇数为男性
                {
                    this.S_7.Text = "女";
                }
                else
                {
                    this.S_7.Text = "男";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("身份证号码输入有误");
                if (S_9.CanFocus)
                {
                    S_9.Focus();
                }
                return;
            }
        }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨水霂

你的鼓励是对我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值