Get birth date and Gender Based on ID card-C # code included

Source: Internet
Author: User

In the past, the information in the ID card number was great. For the 18-digit ID card, the first six represent the location of your household registration, and the seventh to 14th represent the date of birth, from 15th to 17th represents your gender (even numbers are female and odd numbers are male). According to this information, after I enter an employee's ID card in the system, the control focus is transferred and the birthday and gender are obtained based on the ID card number. The code written in C # is as follows:

 

/// <Summary>
/// Define the validity of the ID card number in the validated event of the Control Verification textbox_identitycard and get the birthday and gender according to the ID card number
/// </Summary>
Private void textbox_identitycard_validated (Object sender, eventargs E)
{
Try
{
String identitycard = textbox_identitycard.text.trim (); // obtain the entered ID number.

If (string. isnullorempty (identitycard ))
{
MessageBox. Show ("ID card number cannot be blank! "); // ID card number cannot be blank. If it is blank, return
If (textbox_identitycard.canfocus)
{
Textbox_identitycard.focus (); // sets the current input focus to textbox_identitycard
}
Return;
}
Else
{
If (identitycard. length! = 15 & identitycard. length! = 18) // the ID card number can only be 15 or 18 other illegal characters
{
MessageBox. Show ("the ID card number is 15 or 18 characters. Please check it! ");
If (textbox_identitycard.canfocus)
{
Textbox_identitycard.focus ();
}
Return;
}
}
String Birthday = "";
String sex = "";
If (identitycard. Length = 18) // process the 18-digit ID card number to get the birthday and gender code from the number
{
Birthday = identitycard. substring (6, 4) + "-" + identitycard. substring (10, 2) + "-" + identitycard. substring (12, 2 );
Sex = identitycard. substring (14, 3 );
}
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) // The gender code is an even number and the female odd number is male.
{
This. combobox_sex.text = "";
}
Else
{
This. combobox_sex.text = "male ";
}
}
Catch (exception ex)
{
MessageBox. Show ("Incorrect ID card number entered ");
If (textbox_identitycard.canfocus)
{
Textbox_identitycard.focus ();
}
Return;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.