C#根據(jù)身份證號(hào)碼判斷出生日期和性別
18位的身份證,前面六位代表了你戶籍所在地,第七位到第十四位代表了你的出生年月,第十五位到第十七為代表了你的性別(偶數(shù)為女,奇數(shù)為男),根據(jù)這一信息,我在系統(tǒng)開發(fā)的錄入員工的身份證后控件焦點(diǎn)轉(zhuǎn)移時(shí)根據(jù)身份證號(hào)碼獲得生日和性別。
用C#寫的代碼如下:
/// <summary> /// 在控件驗(yàn)證 textBox_IdentityCard 的 Validated事件中定義身份證號(hào)碼的合法性并根據(jù)身份證號(hào)碼得到生日和性別 /// </summary> private void textBox_IdentityCard_Validated(object sender, EventArgs e) { try { //獲取得到輸入的身份證號(hào)碼 string identityCard = textBox_IdentityCard.Text.Trim(); if (string.IsNullOrEmpty(identityCard)) { //身份證號(hào)碼不能為空,如果為空返回 MessageBox.Show("身份證號(hào)碼不能為空!"); if (textBox_IdentityCard.CanFocus) { textBox_IdentityCard.Focus();//設(shè)置當(dāng)前輸入焦點(diǎn)為textBox_IdentityCard } return; } else { //身份證號(hào)碼只能為15位或18位其它不合法 if (identityCard.Length != 15 && identityCard.Length != 18) { MessageBox.Show("身份證號(hào)碼為15位或18位,請(qǐng)檢查!"); if (textBox_IdentityCard.CanFocus) { textBox_IdentityCard.Focus(); } return; } } string birthday = ""; string sex = ""; //處理18位的身份證號(hào)碼從號(hào)碼中得到生日和性別代碼 if (identityCard.Length == 18) { birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2); sex = identityCard.Substring(14, 3); }<br> //處理15位的身份證號(hào)碼從號(hào)碼中得到生日和性別代碼 if (identityCard.Length == 15) { birthday = "19" + identityCard.Substring(6, 2) + "-" + identityCard.Substring(8, 2) + "-" + identityCard.Substring(10, 2); sex = identityCard.Substring(12, 3); }<br> textBox_Birthday.Text = birthday; //性別代碼為偶數(shù)是女性奇數(shù)為男性 if (int.Parse(sex) % 2 == 0) { this.comboBox_Sex.Text = "女"; } else { this.comboBox_Sex.Text = "男"; } } catch (Exception ex) { MessageBox.Show("身份證號(hào)碼輸入有誤"); if (textBox_IdentityCard.CanFocus) { textBox_IdentityCard.Focus(); } return; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WPF實(shí)現(xiàn)帶篩選功能的DataGrid
在默認(rèn)情況下,WPF提供的DataGrid僅擁有數(shù)據(jù)展示等簡(jiǎn)單功能,如果要實(shí)現(xiàn)像Excel一樣復(fù)雜的篩選過濾功能,則相對(duì)比較麻煩。本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述如何通過WPF實(shí)現(xiàn)DataGrid的篩選功能,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正2023-03-03基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)實(shí)例
這篇文章主要給大家介紹了基于C#實(shí)現(xiàn)的多邊形沖突檢測(cè)的相關(guān)資料,文中介紹的方法并未使用第三方類庫,可以完美解決這個(gè)問題,需要的朋友可以參考下2021-07-07DataGridView凍結(jié)列或行、列順序調(diào)整、操作行頭列頭標(biāo)題的方法
這篇文章介紹了DataGridView凍結(jié)列或行、列順序調(diào)整、操作行頭列頭標(biāo)題的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02

c#生成站點(diǎn)地圖(SiteMapPath)文件示例程序

深入C#任務(wù)管理器中應(yīng)用程序選項(xiàng)隱藏程序本身的方法詳解

C#實(shí)現(xiàn)日期格式轉(zhuǎn)換的公共方法類實(shí)例