C#根据身份证号码判断出生日期和性别
作者:※WYF※
这篇文章主要为大家详细介绍了C#根据身份证号码判断出生日期和性别的方法,感兴趣的小伙伴们可以参考一下
18位的身份证,前面六位代表了你户籍所在地,第七位到第十四位代表了你的出生年月,第十五位到第十七为代表了你的性别(偶数为女,奇数为男),根据这一信息,我在系统开发的录入员工的身份证后控件焦点转移时根据身份证号码获得生日和性别。
用C#写的代码如下:
/// <summary>
/// 在控件验证 textBox_IdentityCard 的 Validated事件中定义身份证号码的合法性并根据身份证号码得到生日和性别
/// </summary>
private void textBox_IdentityCard_Validated(object sender, EventArgs e)
//获取得到输入的身份证号码
string identityCard = textBox_IdentityCard.Text.Trim();
if (string.IsNullOrEmpty(identityCard))
//身份证号码不能为空,如果为空返回
MessageBox.Show("身份证号码不能为空!");
if (textBox_IdentityCard.CanFocus)
textBox_IdentityCard.Focus();//设置当前输入焦点为textBox_IdentityCard
return;
//身份证号码只能为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 = "女";
this.comboBox_Sex.Text = "男";
catch (Exception ex)
MessageBox.Show("身份证号码输入有误");
if (textBox_IdentityCard.CanFocus)
textBox_IdentityCard.Focus();
return;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
Unity 制作一个分数统计系统
2021-11-11
C# StackExchange.Redis 用法汇总
2021-11-11
基于C#实现端口扫描器(单线程和多线程)
2021-11-11
C# Quartzs定时器的使用教程
2021-11-11
美国设下计谋,用娘炮文化重塑日本,已影响至中国
2021-11-19
时空伴随者是什么意思?时空伴随者介绍
2021-11-09
工信部称网盘企业免费用户最低速率应满足基本下载需求,天翼云盘回应:坚决支持,始终
2021-11-05
2022年放假安排出炉:五一连休5天 2022年所有节日一览表
2021-10-26
电脑版
-
返回首页
2006-2023 脚本之家 JB51.Net , All Rights Reserved.
苏ICP备14036222号