Java Regular validation

Source: Internet
Author: User

1     /**2 * Verify the phone number.3      * 4      * @paramPhone the phone5      * @returntrue, if successful6      */7      Public Static BooleanIsphone (String phone) {8         if(Stringutils.isblank (phone)) {9             return false;Ten         } OneString regex = "^[1][3,4,5,8][0-9]{9}$"; APattern p =pattern.compile (regex); -Matcher m =p.matcher (phone); -         returnm.matches (); the}
1     /**2 * Verify that the ID number is legal (only 18 bits are verified).3      *4      * @paramIdcard the ID card5      * @returntrue, if is ID card6      */7      Public Static BooleanIsidcard (String idcard) {8String regex = "((1[1-5) | ( 2[1-3]) | (3[1-7]) | (4[1-6]) | (5[0-4]) | (6[1-5]) |71| (8[12]) |91) \\d{4} ((19\\d{2} (0[13-9]|1[012]) (0[1-9]|[ 12]\\D|30)) | (19\\d{2} (0[13578]|1[02]) 31) | (19\\d{2}02 (0[1-9]|1\\d|2[0-8)) | (19 ([13579][26]|[ 2468][048]|0[48]) (0229)) (\\d{3}) (\\d| x|x)) ";9         int[] Factorarray =New int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};Tenstring[] Paritybit =Newstring[]{"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"}; Onestring[] IDArray =NewString[18]; A         intIntstrlen =idcard.length (); -String Idstr =Idcard; -         intsum = 0; the          -         //Regular verify that the ID card format is correct -Pattern pattern =pattern.compile (regex); -Matcher Matcher =Pattern.matcher (idcard); +         if(!matcher.matches ()) { -             return false; +         } A          at         //Verify that the ID number algorithm is legal -          for(inti = 0; i < Intstrlen; i + +){ -Idarray[i] = Idstr.charat (i) + ""; -             if(I < (intStrLen-1)){ -Sum + = factorarray[i] *Integer.parseint (Idarray[i]); -             } in         } -         if(!paritybit[(sum%)].equalsignorecase (idarray[intstrlen-1])){ to             return false; +         } -         return true; the}

Java Regular validation

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.