1. 程式人生 > 其它 >C# 根據身份證號碼判斷出生日期和性別

C# 根據身份證號碼判斷出生日期和性別

                //獲取得到輸入的身份證號碼
                string identityCard = textBox_IdentityCard.Text.Trim();

                if (string.IsNullOrEmpty(identityCard))
                {
                    //身份證號碼不能為空,如果為空返回
                    MessageBox.Show("身份證號碼不能為空!");
                    if (textBox_IdentityCard.CanFocus)
                    {
                        textBox_IdentityCard.Focus();
//設定當前輸入焦點為textBox_IdentityCard } return; } else { //身份證號碼只能為15位或18位其它不合法 if (identityCard.Length != 15 && identityCard.Length != 18) { MessageBox.Show(
"身份證號碼為15位或18位,請檢查!"); if (textBox_IdentityCard.CanFocus) { textBox_IdentityCard.Focus(); } return; } } string birthday = ""
; string sex = ""; //處理18位的身份證號碼從號碼中得到生日和性別程式碼 if (identityCard.Length == 18) { birthday = identityCard.Substring(6, 4) + "-" + identityCard.Substring(10, 2) + "-" + identityCard.Substring(12, 2); sex = identityCard.Substring(14, 3); } //處理15位的身份證號碼從號碼中得到生日和性別程式碼 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 = ""; }