C#–添加对象前 – 身份证号的验证

验证是否符合通用验证类的要求

调用通用验证类


通过数据验证


验证身份证号是否存在SQL

如果SQL有了身份证信息新增的身份证号不能是重复的

编写查询身份证方法

 /// <summary> /// 查询身份证是否已经存在 /// </summary> /// <param name="idcard"></param> /// <returns></returns> public bool IsIdCardExisted(string idcard) { string sql = "Select count(*) from Students where StudentIdNo={0}"; sql = string.Format(sql, idcard); int count = Convert.ToInt32(SQLHelper.GetSingleResult(sql)); if (count == 1) return true; else return false; }

通过数据验证

SQL已经有了存在的身份证号,新增的身份证号重复不通过


验证身份证是否与出生日期吻合

  //验证身份证号是否和出生日期吻合
            string month = string.Empty; string day = string.Empty; if(Convert.ToDateTime(this.txtBirthday.Text).Month<10) month = "0" + Convert.ToDateTime(this.txtBirthday.Text).Month.ToString(); else month = Convert.ToDateTime(this.txtBirthday.Text).Month.ToString(); if (Convert.ToDateTime(this.txtBirthday.Text).Day < 10) day = "0" + Convert.ToDateTime(this.txtBirthday.Text).Day.ToString(); else day = Convert.ToDateTime(this.txtBirthday.Text).Day.ToString(); string birthday = Convert.ToDateTime(this.txtBirthday.Text).Year.ToString() + month + day; if(!this.txtStuIdNo.Text.Trim().Contains(birthday)) { MessageBox.Show("身份证号和出生日期不匹配", "验证提示"); this.txtStuIdNo.Focus(); this.txtStuIdNo.SelectAll(); return; }