China ID Card number Verification

Source: Internet
Author: User
Tags bool datetime numeric pow split

Using System;

<summary>
Summary description for Idcardvalid
</summary>
public class Idcardvalid
{
Public Idcardvalid ()
{
//
Todo:add constructor Logic here
//
}

<summary>
Verify ID Number
</summary>
<param name= "id" > ID number </param>
<returns> Verify success is true, otherwise false</returns>
public static bool Checkidcard (string Id)
{
if (id.length = 18)
{
BOOL check = CHECKIDCARD18 (Id);
return check;
}
else if (id.length = 15)
{
BOOL check = CHECKIDCARD15 (Id);
return check;
}
Else
{
return false;
}
}
#region ID Number Verification

///<summary>
///Verify the 15-bit identity number
///</summary>
///<param name= "id" > ID number </param>
///<returns> Validation succeeded to true, otherwise false</returns>
private static bool CheckIDCard18 (string Id)
{
Long n = 0;
if (long. TryParse (Id.remove), out n) = = False | | N < Math.pow (10, 16) | | Long. TryParse (Id.replace (' x ', ' 0 '). Replace (' X ', ' 0 '), out n) = = false]
{
Return false;//numeric validation
}
String address = "11x22x35x44x53x12x23x36x45x5 4x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91 ";
if (address. IndexOf (Id.remove (2)) = = 1)
{
return false;//province validation
}
String birth = id.substring (6, 8). Insert (6, "-"). Insert (4, "-");
DateTime time = new DateTime ();
if (datetime.tryparse (birth) = = False)
{
Return false;//birthday authentication
}
string[] Arrvarifycode = ("1 , 0,x,9,8,7,6,5,4,3,2 "). Split (', ');
String[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2"). Split (', ');
char[] Ai = Id.removE (17). ToCharArray ();
int sum = 0;
for (int i = 0; i < i++)
{
sum = Int. Parse (Wi[i]) * Int. Parse (Ai[i]. ToString ());
}
Int y =-1;
Math.divrem (sum, one, out y);
if (Arrvarifycode[y]!= id.substring (17, 1). ToLower ())
{
Return false;//check code validation
}
Return true;//compliant with gb11643-1999 standard
}

///<summary>
///Verify the 18-bit identity number
///</summary>
///<param name= "id" > ID number </param>
///<returns> Validation succeeded to true, otherwise false</returns>
private static bool CheckIDCard15 (string Id)
{
Long n = 0;
if (long. TryParse (Id, out n) = = False | | n < Math.pow)
{
Return false;//numeric validation
}
String address = "11x22x35x44x53x12x23x36x45x54x13x31x37 X46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91 ";
if (address. IndexOf (Id.remove (2)) = = 1)
{
return false;//province validation
}
String birth = id.substring (6, 6). Insert (4, "-"). Insert (2, "-");
DateTime time = new DateTime ();
if (Datetime.tryparse (birth, out time) = = False)
{
Return false;//birthday authentication
}
Return true;//compliant with 15-bit ID standard
}
#endregion
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.