首页 国内 国际 娱乐 体育 财经 科技 留学 健康 论坛 空间 汽车 文学 军事 爱墙 游戏 艺术 星座
短信 明星 数码 手机 美食 家居 股票 教育 公益 公益 旅游 彩票 商城 校园 奇闻 女性 房产 时尚

录入学员的身份证后控件焦点转移时根据身份证号码获得生日和性别

自从接触了报名系统,认证系统,才知道身份证号码里面的信息大有乾坤,以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_IdentityCar

                   }
                   
return;
                   }
               
else
               {
                   
if (identityCard.Length != 15 && identityCard.Length != 18)//身份证号码只能为15位或18位其它不合法
                   {

                       MessageBox.Show(
"身份证号码为15位或18位,请检查!");

                       
if (textBox_IdentityCard.CanFocus)
                       {
                           textBox_IdentityCard.Focus();
                       }
                       
return;
                    }
               }

               
string birthday = "";
               
string sex = "";
               
if (identityCard.Length == 18)//处理18位的身份证号码从号码中得到生日和性别代码
              {
                   birthday 
= identityCard.Substring(64+ "-" + identityCard.Substring(102+ "-" + identityCard.Substring(122);
                   sex 
= identityCard.Substring(143);
               }
               
if (identityCard.Length == 15)
               {

                   birthday 
= "19" + identityCard.Substring(62+ "-" + identityCard.Substring(82+ "-" + identityCard.Substring(102);
                   sex 
= identityCard.Substring(123);
               }
               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;
           }
       }

posted @ 2009-03-02 15:08  程序执着追随者  阅读(726)  评论(1编辑  收藏  举报
友情链接
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置
你的位置