C#依據身份證號碼斷定出身日期和性別。本站提示廣大學習愛好者:(C#依據身份證號碼斷定出身日期和性別)文章只能為提供參考,不一定能成為您想要的結果。以下是C#依據身份證號碼斷定出身日期和性別正文
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);
}<br>
//處置15位的身份證號碼從號碼中獲得誕辰和性別代碼
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;
//性別代碼為偶數是女性奇數為男性
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;
}
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。