C # Example analysis of verifying identity card numbers (correctness)

Source: Internet
Author: User
This article mainly describes the C # verification ID card number correctness of the instance code, including 18-digit number and 15-digit number verification, the need for friends can refer to the following

Today in the QQ space to see a C # language verification 18 ID card number verification method and instance code, smoked some time to learn a bit, personally feel good, so put it down, easy to use later!

18-digit number:


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; The string address = "      11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91 "; if (address.      IndexOf (Id.remove (2)) = =-1) {return false; } String birth = Id.substring (6, 8). Insert (6, "-").      Insert (4, "-");      DateTime time = new DateTime ();      if (Datetime.tryparse (birth, out time) = = False) {return false; } 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.prem (sum, one, out y); if (arrvarifycode[y]! = id.substring (17, 1).      ToLower ()) {return false; } return true;//Correct}

15-digit number:


private static bool CheckIDCard15 (string Id)    {      long n = 0;      if (long. TryParse (Id, out n) = = False | | N < Math.pow (+))      {        return false;      }      string address = " 11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91 ";      if (address. IndexOf (Id.remove (2)) = =-1)      {        return false;      }      string birth = id.substring (6, 6). Insert (4, "-"). Insert (2, "-");      DateTime time = new DateTime ();      if (Datetime.tryparse (birth, out time) = = False)      {        return false;      }      return true;//correct    }

Summarize

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.