Copy Code code as follows:
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-digit ID number
</summary>
<param name= "id" > ID number </param>
<returns> Verify success is 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;//Digital 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 = ("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 verification
}
Return true;//meet gb11643-1999 Standard
}
<summary>
Verify the 18-digit ID number
</summary>
<param name= "id" > ID number </param>
<returns> Verify success is true, otherwise 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;//Digital Verification
}
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;//conforms to the 15-digit identity card standard
}
#endregion
}