/// <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;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有