Java authentication identity card format and legality

Source: Internet
Author: User

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

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.