Check whether the ID card is valid c # code

Source: Internet
Author: User

/// <Summary>
/// Verify the 18-digit ID card number
/// </Summary>
/// <Param name = "Id"> Id card number </param>
/// <Returns> If the verification succeeds, the value is True; otherwise, the value is False. </returns>
Private static bool CheckIDCard18 (string Id)
{
Long n = 0;
If (long. tryParse (Id. remove (17), out n) = false | n <Math. pow (10, 16) | long. tryParse (Id. replace ('x', '0 '). replace ('x', '0'), out n) = false)
{
Return false; // digit Verification
}
String address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91 ";
If (address. IndexOf (Id. Remove (2) =-1)
{
Return false; // province Verification
}
String birth = Id. Substring (6, 8). Insert (6, "-"). Insert (4 ,"-");
DateTime time = new DateTime ();
If (DateTime. TryParse (birth, out time) = false)
{
Return false; // birthday Verification
}
String [] arrVarifyCode = (", 0, x, 9, 8, 4,"). Split (',');
String [] Wi = (","). Split (',');
Char [] Ai = Id. Remove (17). ToCharArray ();
Int sum = 0;
For (int I = 0; I <17; I ++)
{
Sum + = int. Parse (Wi [I]) * int. Parse (Ai [I]. ToString ());
}
Int y =-1;
Math. DivRem (sum, 11, out y );
If (arrVarifyCode [y]! = Id. Substring (17, 1). ToLower ())
{
Return false; // Verification Code
}
Return true; // conforms to GB11643-1999 standards
}


/// <Summary>
/// Verify the 15-digit ID card number
/// </Summary>
/// <Param name = "Id"> Id card number </param>
/// <Returns> If the verification succeeds, the value is True; otherwise, the value is False. </returns>
Private static bool CheckIDCard15 (string Id)
{
Long n = 0;
If (long. TryParse (Id, out n) = false | n <Math. Pow (10, 14 ))
{
Return false; // digit Verification
} Www.2cto.com
String address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91 ";
If (address. IndexOf (Id. Remove (2) =-1)
{
Return false; // province Verification
}
String birth = Id. Substring (6, 6). Insert (4, "-"). Insert (2 ,"-");
DateTime time = new DateTime ();
If (DateTime. TryParse (birth, out time) = false)
{
Return false; // birthday Verification
}
Return true; // meets the 15-digit ID card Standard
}

Related Article

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.