录入学员的身份证后控件焦点转移时根据身份证号码获得生日和性别
生活随笔
收集整理的這篇文章主要介紹了
录入学员的身份证后控件焦点转移时根据身份证号码获得生日和性别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
自從接觸了報名系統,認證系統,才知道身份證號碼里面的信息大有乾坤,以18位的身份證來說,前面六位代表了你戶籍所在地,第七位到第十四位代表了你的出生年月,第十五位到第十七為代表了你的性別(偶數為女,奇數為男),根據這一信息,我在系統開發的錄入學員的身份證后控件焦點轉移時根據身份證號碼獲得生日和性別,用C#寫的代碼如下:
?
?///?<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_IdentityCar
???????????????????}
???????????????????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;
???????????}
???????}
?
?///?<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_IdentityCar
???????????????????}
???????????????????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;
???????????}
???????}
轉載于:https://www.cnblogs.com/wwl3325/archive/2009/03/02/1401540.html
總結
以上是生活随笔為你收集整理的录入学员的身份证后控件焦点转移时根据身份证号码获得生日和性别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ2140
- 下一篇: TCP 端口监听队列原理