Recent project new features, need to mobile phone number, name, ID card number and other information to verify, the best way is through regular expression to verify, on-line check some information, wrote these tools methods.
1. Verify the phone number
Rule: The first digit can only be 1, the second digit is 3-8, 3-11 bits is any number
/** Mobile phone number field check, 1th digit: 1; 2nd bit: {3, 4, 5, 6, 7, 8} any number; 第3-11位: 0-9 any number * @param value * @return*/ Public Staticboolean istelphonenumber (String value) {if(Value! =NULL&& value.length () = = One) {pattern pattern= Pattern.compile ("^1[3|4|5|6|7|8][0-9]\\d{8}$"); Matcher Matcher=Pattern.matcher (value); returnmatcher.matches (); } return false; }
2. Name Verification The name is verified here, and the user can enter anything in the input box, but this method is adjusted when clicking on the verification button.
The validation rules are: Names are added by Chinese or Chinese characters "?","· "The composition, and," point "can only have one," point "position can not be in the first and not at the end, only between the characters will be verified through.
/** * Verify that the name entered is "Chinese" or whether it contains "•"*/ Public Staticboolean 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 number
Verify the ID number
The rule is: consists of 15 digits or 18 digits (17 digits Plus "x"), 15-bit pure numbers nothing to say, 18-bit words, can be 18-bit pure numbers, or 17 digits plus "x"
/* * * Verify that the entered ID number is valid */public static boolean islegalid (String ID) { if (Id.touppercase (). Matches ("(^\\d{15}$) | ( ^\\D{17} ([0-9]| X) ($)){ returntrue; } Else { returnfalse; } }
The above regular expression validates the result, with true and false returns
Android Regular expression verifies phone number, name (including minority), Social Security number