Android Regular Expression verifies mobile phone numbers, names (including ethnic minorities), ID card numbers, and android Regular Expressions

Source: Internet
Author: User

Android Regular Expression verifies mobile phone numbers, names (including ethnic minorities), ID card numbers, and android Regular Expressions

The new features in recent projects require verification of mobile phone numbers, names, ID card numbers, and other information. The best way is to use regular expressions for verification and check some information online, write these tools and methods.

1. Verify the mobile phone number

Rule: the first digit can only be 1, the second digit is a number in 3-8, and the third digit is any number.

/*** Mobile phone number verification, 1st bits: 1; 2nd bits: {3, 4, 5, 6, 7, 8} Any number; 3-11 bits: 0-9 arbitrary number * @ param value * @ return */public static boolean isTelPhoneNumber (String value) {if (value! = Null & value. length () = 11) {Pattern pattern = Pattern. compile ("^ 1 [3 | 4 | 5 | 6 | 7 | 8] [0-9] \ d {8} $"); Matcher matcher = pattern. matcher (value); return matcher. matches ();} return false ;}

2. Verify the name here. You can enter anything in the input box, but this method is called when you click the Verify button.

The verification rules are as follows: the name consists of Chinese characters or Chinese characters plus "•" and "·", and only one "vertex" can be entered, the "point" cannot be in the first or end position. It is verified only between Chinese characters.

/*** Verify whether the input name is "Chinese" or "·" */public static boolean isLegalName (String name) {if (name. contains ("·") | name. contains ("•") {if (name. matches ("^ [\ u4e00-\ u9fa5] + [· •] [\ u4e00-\ u9fa5] + $") {return true ;} else {return false ;}} else {if (name. matches ("^ [\ u4e00-\ u9fa5] + $") {return true ;}else {return false ;}}}

3. Verify the ID card number

Verify ID card number

The rule is: it consists of 15 digits or 18 digits (17 digits plus "x"). There is nothing to say about 15 pure digits, it can be an 18-digit numeric, or a 17-digit numeric plus "x"

/*** Verify that the entered id card number is valid */public static boolean isLegalId (String id) {if (id. toUpperCase (). matches ("(^ \ d {15} $) | (^ \ d {17} ([0-9] | X) $ )")) {return true;} else {return false ;}}

The above regular expression is used to verify the result. true and false are returned.

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.