asp.net China ID card number verification code non-regular _ practical skills

Source: Internet
Author: User
Tags pow
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
}
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.