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