The example in this paper describes the method of C # to implement ID number verification. Share to everyone for your reference. The implementation method is as follows:
With the development of the Internet now, more and more registered users of the place to use the identity card, then for the input ID card how to verify it? Look at the code below, it's actually very simple.
The main note is that the current identity card is divided into 16 and 18 bits, and then verify the check digit, province, birthday can be.
The main classes are as follows:
/// <summary> ///Verify the ID number class/// </summary> Public classidcardvalidation {/// <summary> ///verifying the identity card rationality/// </summary> /// <param name= "Id" ></param> /// <returns></returns> Public BOOLCheckidcard (stringIdNumber) { if(Idnumber.length = = -) { BOOLCheck =CheckIDCard18 (IdNumber); returncheck; } Else if(Idnumber.length = = the) { BOOLCheck =CheckIDCard15 (IdNumber); returncheck; } Else { return false; } } /// <summary> ///18-digit ID number verification/// </summary> Private BOOLCheckIDCard18 (stringIdNumber) { Longn =0; if(Long. TryParse (Idnumber.remove ( -), outN) = =false|| N < Math.pow (Ten, -) ||Long. TryParse (Idnumber.replace ('x','0'). Replace ('X','0'), outN) = =false) { return false;//Digital Verification } stringAddress ="11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; if(Address. IndexOf (Idnumber.remove (2)) == -1) { return false;//Province Verification } stringBirth = idnumber.substring (6,8). Insert (6,"-"). Insert (4,"-"); DateTime Time=NewDateTime (); if(Datetime.tryparse (Birth, outtime) = =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 = Idnumber.remove ( -). ToCharArray (); intsum =0; for(inti =0; I < -; i++) {sum+=int. Parse (Wi[i]) *int. Parse (Ai[i]. ToString ()); } inty =-1; Math.divrem (SUM, One, outy); if(Arrvarifycode[y]! = idnumber.substring ( -,1). ToLower ()) {return false;//Check Code Verification } return true;//complies with gb11643-1999 standards } /// <summary> ///16-digit ID number verification/// </summary> Private BOOLCHECKIDCARD15 (stringIdNumber) { Longn =0; if(Long. TryParse (IdNumber, outN) = =false|| N < Math.pow (Ten, -)) { return false;//Digital Verification } stringAddress ="11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; if(Address. IndexOf (Idnumber.remove (2)) == -1) { return false;//Province Verification } stringBirth = idnumber.substring (6,6). Insert (4,"-"). Insert (2,"-"); DateTime Time=NewDateTime (); if(Datetime.tryparse (Birth, outtime) = =false) { return false;//Birthday Verification } return true; }} Test call: Copy code code as follows: Idcardvalidation card=Newidcardvalidation ();//from the online identity card Daquan, verify that the result is true BOOLresult = Card. Checkidcard ("522324197508045617"); Console.WriteLine (Result. ToString ()); //random, verify the result is falseresult = Card. Checkidcard ("612427199901281214"); Console.WriteLine (Result. ToString ()); Console.ReadLine ();
Test Call:
Test Call:
New// from the online ID Daquan, verify that the result is true bool result = Card. Checkidcard ("522324197508045617"); Console.WriteLine (Result. ToString ()); // random, verify the result is False result = Card. Checkidcard ("612427199901281214"); Console.WriteLine (Result. ToString ()); Console.ReadLine ();
C # method of implementing ID number verification