Travel e-commerce, book hotel or order tickets will be the identity card as a consumer voucher, in order to prevent customers mistakenly fill in the ID card to bring unnecessary trouble, need to verify the code format and legality, the code is as follows:
/ ** * To determine the format of the ID card * * @param idnum * @return*/ Public Staticboolean isidnum (String idnum) {//Chinese Citizen ID card format: 15 or 18 bits in length, the last one can be a letterPattern Idnumpattern = Pattern.compile ("(\\d{14}[0-9a-za-z]) | (\\d{17}[0-9a-za-z])"); //Format Validation if(!Idnumpattern.matcher (Idnum). Matches ())return false; //Legitimacy Verification intYear =0; intmonth =0; intDay =0; if(idnum.length () = = the) { //Generation IDSystem. out. println ("Generation ID:"+idnum); //extract the first 6 digits on the ID card and date of birthPattern Birthdatepattern = Pattern.compile ("\\d{6} (\\d{2}) (\\d{2}) (\\d{2}). *"); Matcher Birthdatemather=Birthdatepattern.matcher (Idnum); if(Birthdatemather.find ()) { year= Integer.valueof (" +"+ Birthdatemather.group (1)); Month= Integer.valueof (Birthdatemather.group (2)); Day= Integer.valueof (Birthdatemather.group (3)); } } Else if(idnum.length () = = -) { //second-generation ID cardSystem. out. println ("second-generation ID card:"+idnum); //extract the first 6 digits on the ID card and date of birthPattern Birthdatepattern = Pattern.compile ("\\d{6} (\\d{4}) (\\d{2}) (\\d{2}). *"); Matcher Birthdatemather=Birthdatepattern.matcher (Idnum); if(Birthdatemather.find ()) { year= Integer.valueof (Birthdatemather.group (1)); Month= Integer.valueof (Birthdatemather.group (2)); Day= Integer.valueof (Birthdatemather.group (3)); } } //year judgment, 100 years ago so farCalendar Cal=calendar.getinstance (); //Current year intCurrentYear = Cal.Get(calendar.year); if(Year <= CurrentYear- -|| Year >currentyear)return false; //Month judgment if(Month <1|| Month > A) return false; //Date Judgment//calculate the number of months intDaycount = to; Switch(month) { Case 1: Case 3: Case 5: Case 7: Case 8: Case Ten: Case A: Daycount= to; Break; Case 2: //February to determine whether a leap year if(Year%4==0&& Year% -!=0) || (Year% -==0) ) {Daycount= in; Break; } Else{Daycount= -; Break; } Case 4: Case 6: Case 9: Case One: Daycount= -; Break; } System. out. println (String.Format ("Birthday:%d years%d months%d days", year, month, day)); System. out. println (Month +"months are:"+ Daycount +"days"); if(Day <1|| Day >daycount)return false; return true; }
Java authentication identity card format and legitimacy