C# 根據社會安全號碼碼判斷出生日期和性別

來源:互聯網
上載者:User

標籤:

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_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 = "男";                }            }            catch (Exception ex)            {                MessageBox.Show("社會安全號碼碼輸入有誤");                if (textBox_IdentityCard.CanFocus)                {                    textBox_IdentityCard.Focus();                }                return;            }        }

 

C# 根據社會安全號碼碼判斷出生日期和性別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.