private void WorkerID_Validated(object sender, EventArgs e) { //获取得到输入的身份证号码 string identityCard = WorkerID.Text.Trim(); if (string.IsNullOrEmpty(identityCard)) { //身份证号码不能为空,如果为空返回 MessageBox.Show("身份证号码不能为空!"); if (WorkerID.CanFocus) { WorkerID.Focus();//设置当前输入焦点为txtCardID_identityCard } return; } else { //身份证号码只能为15位或18位其它不合法 if (identityCard.Length != 15 && identityCard.Length != 18) { MessageBox.Show("身份证号码为15位或18位,请检查!"); if (WorkerID.CanFocus) { WorkerID.Focus(); } return; } } string birthday = ""; string sex = ""; if (identityCard.Length == 18) { var result = new IDCardUtil().SelectCardCode(identityCard); var address = result.Replace(",", "").ToString(); birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2); sex = identityCard.Substring(14, 3); txtAddress.Text = address; //性别代码为偶数是女性奇数为男性 if (int.Parse(sex) % 2 == 0) { cboSex.SelectedIndex = 0; } else { cboSex.SelectedIndex = 1; } } try { dtpBirthday.Value = Convert.ToDateTime(birthday); } catch { MessageBox.Show("请正确输入证件号码!"); return; } dtpBirthday.Value = Convert.ToDateTime(birthday); return; }
private void WorkerID_Validated(object sender, EventArgs e) { //获取得到输入的身份证号码 string identityCard = WorkerID.Text.Trim(); if (string.IsNullOrEmpty(identityCard)) { //身份证号码不能为空,如果为空返回 MessageBox.Show("身份证号码不能为空!"); if (WorkerID.CanFocus) { WorkerID.Focus();//设置当前输入焦点为txtCardID_identityCard lblChecked.Text = "◆"; lblChecked.ForeColor = Color.Red; lblChecked.Visible = true; } return; } else { //身份证号码只能为15位或18位其它不合法 if (identityCard.Length != 15 && identityCard.Length != 18) { MessageBox.Show("身份证号码为15位或18位,请检查!"); if (WorkerID.CanFocus) { WorkerID.Focus(); lblChecked.Text = "◆"; lblChecked.ForeColor = Color.Red; lblChecked.Visible = true; } return; } } string birthday = ""; string sex = ""; if (identityCard.Length == 18) { MySqlConnection con = DBHelper.GetConnection(); con.Open(); MySqlDataReader dr = DBHelper.ExecuteReader("select Province,City,District from CARDCODES where bm='" + identityCard.Substring(0, 6).ToString() + "'"); birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2); sex = identityCard.Substring(14, 3); while (dr.Read()) { txtAddress.Text = dr["Province"].ToString() + dr["City"].ToString() + dr["District"].ToString(); } //性别代码为偶数是女性奇数为男性 if (int.Parse(sex) % 2 == 0) { cboSex.Text = "女"; } else { cboSex.Text = "男"; } dr.Close(); con.Close(); } dtpBirthday.Value = Convert.ToDateTime(birthday); lblChecked.Text = "◆"; lblChecked.ForeColor = Color.Green; lblChecked.Visible = true; return; }