Package com. Work. util; import java. Io. bufferedreader; import java. Io. inputstreamreader;/*** @ author cuiwx * wangmj sorting optimization. **/Public class idcard {// calculated exponential array. algorithm: Sum of the n-1 power of 2, divided by 11 modulo. // For example: 0 power of 2 divided by 11 modulo = 1 power of 1, 2 divided by 11 modulo = 2 power of 2 divided by 11 modulo = 4 static int [] Wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; // check Bit Array static char [] Ai = {'1', '0', 'x', '9', '8', '7', '6 ', '5', '4', '3', '2'}; public static void main (string ARGs []) {// idcard Ic = new idcard (); // system. currenttimemillis (); try {Boolean flag = false; while (FLAG) {Bufferedreader reader = new bufferedreader (New inputstreamreader (system. in); system. out. println ("15-digit ID card number:"); string lowerid = reader. readline (); If (lowerid. equals ("quit") {system. out. println ("Bye ~~ "); Break;} system. out. println ("8-digit Date Of Birth (19791216):"); string birth = reader. readline (); system. out. println ("Enter gender"); string sex = reader. readline (); system. out. println (checkidnumber (lowerid, birth, sex); system. out. println ("18 bits:" + upperidnumber (lowerid, birth); system. out. println (checkidnumber (upperidnumber (lowerid, birth), birth, sex) ;}} catch (exception e) {system. out. println (e) ;}}/*** Based on the 15-digit ID card number and birthdate Calculate the 18-digit ID card number ** @ Param lowerid, 15-digit ID card number * @ Param birthday date of birth, 19810912 * @ return upperid, returns the 18-digit ID card number */public static string upperidnumber (string lowerid, string birthday) {If (lowerid. length ()! = 15) {return "enter a 15-digit ID card number. ";} Else {return lowerid. substring (0, 6) + birthday. substring (0, 2) + lowerid. substring (6) + AI [checkbit (lowerid, birthday)];} // return lowerid + AI [checkbit ("372832198109126616")];} /*** calculate the check digit ** @ Param lowerid, 15-digit ID Number * @ Param birthday date, 19810912 * @ return mod, and 18th-digit check digit Based on the 15-digit ID card number and birth date, used to take the number from the AI array as the last digit of the ID card number, that is, Ai [mod] */public static int checkbit (string lowerid, string birthday) {If (lowerid. length ()! = 15) // enter the 15-digit ID card number return-1; lowerid = lowerid. substring (0, 6) + birthday. substring (0, 2) + lowerid. substring (6); int sum = 0; // calculate the checksum bit. Divide the first 17 weighted summed values by 11 and Modulo for (INT I = 1; I <lowerid. length () + 1; I ++) {sum = sum + wi [I-1] * (integer. parseint (lowerid. substring (I-1, I);} // system. out. println ("sum =" + sum); // calculate the checkpoint endint mod = sum % 11; return MOD;}/*** Based on the incoming 18-digit ID card number, calculate the checkpoint ** @ Param ID, 18-digit ID card number *@ Return mod: return the check bit. It is used to take the number from the AI array as the last digit of the ID card number, that is, Ai [mod] */public static int checkbit (string ID) {string lowerid = ID. substring (0, 17); int sum = 0; For (INT I = 1; I <lowerid. length () + 1; I ++) {sum = sum + wi [I-1] * (integer. parseint (lowerid. substring (I-1, I);} int mod = sum % 11; return MOD;}/*** verify ID card number ** @ Param ID * ID card number, including 15 and 18 * @ Param birthday birthdate 8 (20071207) * @ Param sex, gender, odd male, even female * @ retur N result, returns whether the ID card number is correct */public static string checkidnumber (string ID, string birthday, string sex) {string result = ""; int Len = ID. length (); If (LEN = 15) {// call the 15-digit ID card number verification method result = checkid_15 (ID, birthday, sex); // system. out. println ("15-bit ---------------");} else if (LEN = 18) {// call the 18-bit ID verification method result = checkid_18 (ID, birthday, sex ); // system. out. println ("18-bit ---------------");} else {result = "false | The length of the ID card number is incorrect. It can only be 15 or 18 characters. ";} Return result;}/*** verify the 15-digit ID card number ** @ Param ID * 15-digit ID card number * @ Param birthday birth date 8-digit (20071207) * @ Param sex, gender, male is odd, female is even * @ return result */public static string checkid_15 (string ID, string birthday, string sex) {string result = ""; string birth_id = ID. substring (6, 12); // 6-digit date string birth = birthday. substring (2); If (birth_id.equals (birth) {// Date of the test. The date of birth matches the date of birth in the ID card, and then the sex if (sex. equals ("male") {// Gender: male, The last digit is an odd string temp = ID. substring (14); // the last digit represents the Gender int isex = integer. parseint (temp); If (isex % 2 = 1) {// the remainder of Division 2 is 1 result = "True | the ID card number is verified. ";} Else {result =" false | the ID card number does not match the gender. ";}} Else if (sex. equals ("female") {// The Gender is female, and the last digit is an even string temp = ID. substring (14); // the last digit represents the Gender int isex = integer. parseint (temp); If (isex % 2 = 0) {// the remainder of Division 2 is 0 result = "True | the ID card number is verified. ";} Else {result =" false | the ID card number does not match the gender. ";}} Else {result =" false | Gender input error. ";}} Else {// The date of birth does not match the date of birth in the ID card result =" false | the date of birth does not match the date of birth in the ID card. ";} Return result;}/*** verify the 18-digit ID card number ** @ Param ID * 18-digit ID card number * @ Param birthday birth date 8-digit (20071207) * @ Param sex, gender, male is odd, female is even * @ return result */public static string checkid_18 (string ID, string birthday, string sex) {string result = ""; string birth_id = ID. substring (6, 14); // 8-digit date if (birth_id.equals (birthday) {// check date. The date of birth matches the date of birth in the ID card, then test the gender if (sex. equals ("male") {// The Gender is male, and the last digit is an odd string temp = ID. substring (16, 17); // the second to last represents the Gender int isex = integer. parseint (temp); If (isex % 2 = 1) {// the remainder of Division 2 is 1 result = "True | the ID card number is verified. ";} Else {result =" false | the ID card number does not match the gender. ";}} Else if (sex. equals ("female") {// The Gender is female, and the last digit is an even string temp = ID. substring (16, 17); // the second to last represents the Gender int isex = integer. parseint (temp); If (isex % 2 = 0) {// the remainder of Division 2 is 0 result = "True | the ID card number is verified. ";} Else {result =" false | the ID card number does not match the gender. ";}} Else {result =" false | Gender input error. ";}} Else {// The date of birth does not match the date of birth in the ID card result =" false | the date of birth does not match the date of birth in the ID card. ";}Return result ;}}
JS verifies the ID card
// Function idcode15to18 (scode15) {// alert ("START"); var arrverifycode = [, "X, 5, 4, 3, 2]; var Wi = [, 5,]; var I = 0, sum = 0, code; vaR idcode = scode15.substr () + "19" + scode15.substr (6); // alert (idcode); // calculate the check bit, the first 17 weighted sums, divide by 11 and Modulo for (I = 1; I <= idcode. length; I ++) {// alert (idcode. substr (I-1, 1); sum = sum + wi [I-1] * parseint (idcode. substr (I-1, 1 )) ;} // Sum = 14; sum = sum % 11; return idcode + arrverifycode [Sum];} function checkid (PID) {var arrverifycode = [1, 0, "X ", 9, 8,]; var Wi = [,]; If (PID. length! = 15 & pid. length! = 18) Return "ID card No. has a total of 15 codes or 18 digits"; var AI = PID. Length = 18? PID. substring (): PID. Slice () + "19" + PID. Slice (); If (! /^/D + $/. Test (AI) Return "the ID card must be a number except the last one! "; Var yyyy = AI. slice (6, 10), Mm = AI. slice (10, 12)-1, DD = AI. slice (12, 14); var d = new date (yyyy, mm, DD), Year = D. getfullyear (), MON = D. getmonth (), Day = D. getdate (), now = new date (); If (year! = Yyyy | mon! = Mm | day! = Dd | D> now | now. getfullyear ()-year> 140) Return "Incorrect ID card! "; For (VAR I = 0, ret = 0; I <17; I ++) RET + = AI. charat (I) * WI [I]; AI + = arrverifycode [RET % = 11]; return PID. length = 18 & pid. tolowercase ()! = Ai? "The last digit of the ID card failed to be verified. The ID card entered incorrectly! It should be "+ arrverifycode [RET % = 11] +". Please check carefully. ": AI ;}; ========================================================== ====================================== if (form1.sfzhm. value. trim () = "") {alert ("ID card number cannot be blank"); form1.sfzhm. focus (); Return false;} else {// verify the correctness of the ID card number based on the region, gender, and date of birth. var tempsfzhm = form1.sfzhm. value. trim (); tempsfzhm = checkid (tempsfzhm); tempsfzhm = tempsfzhm. tolowercase (); // This entry has automatically converted the ID card number to lowercase. // The following judgment has a problem // tempsfzhm. Length = 18 and the last digit is X, then the judgment will fail! // Alert (tempsfzhm. Length = 18 & tempsfzhm. substr (17,1) = 'X'); If ((! (Tempsfzhm. Length = 18 & tempsfzhm. substr (17,1) = 'X') & isnan (tempsfzhm) {alert ("ID card number error! "+ Tempsfzhm); form1.sfzhm. focus (); Return false;} else {var id = string (tempsfzhm); If (ID. length = 15) {id = idcode15to18 (ID);} // alert (ID); var dessex = form1.xb. value. trim (); // gender var desszdq = form1.szdq. value. trim (); // region var descsrq = form1.csrq. value. trim (); // Date of Birth var tempsex = ID. slice (14,17) % 2? "Male": "female"; if (tempsex = "male") {tempsex = "1" ;}else {tempsex = "0" ;}var tempcsrq = ID. substr (6, 4) + "/" + id. substr (10, 2) + "/" + id. substr (12, 2); // alert (tempcsrq); // var tempszdq = ID. substr (0, 2); // var tempszdq2 = ID. substr (0, 4); // var tempszdq3 = ID. substr (0, 6); If (dessex! = Tempsex) {alert ("Incorrect ID number! It is inconsistent with gender. "); Form1.sfzhm. Focus (); Return false;} If (descsrq! = Tempcsrq) {alert ("Incorrect ID number! Does not match the date of birth. "); Form1.sfzhm. Focus (); Return false;} // If (desszdq! = Tempszdq & desszdq! = Tempszdq2 & desszdq! = Tempszdq3) {// alert ("Incorrect ID card number! Does not match the region. "); // Form1.sfzhm. Focus (); // return false; // Finally, 15-bit is upgraded to 18-bit and saved to the database. // Alert ("start to convert to ascending position"); document. Forms [0]. sfzhm. value = ID. tolowercase ();}}
==================== Below is the Java program ========================= ======================================