java验证手机的工具类

 1 package util;
 2 
 3 import java.util.regex.Matcher;
 4 import java.util.regex.Pattern;
 5 import java.util.regex.PatternSyntaxException;
 6 
 7 public class PhoneFormatCheckUtils {
 8 
 9     /** 
10      * 大陆号码或香港号码均可 
11      */  
12     public static boolean isPhoneLegal(String str)throws PatternSyntaxException {  
13         return isChinaPhoneLegal(str) || isHKPhoneLegal(str);  
14     }  
15   
16     /** 
17      * 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数 
18      * 此方法中前三位格式有: 
19      * 13+任意数 
20      * 15+除4的任意数 
21      * 18+除1和4的任意数 
22      * 17+除9的任意数 
23      * 147 
24      */  
25     public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {  
26         String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";  
27         Pattern p = Pattern.compile(regExp);  
28         Matcher m = p.matcher(str);  
29         return m.matches();  
30     }  
31   
32     /** 
33      * 香港手机号码8位数,5|6|8|9开头+7位任意数 
34      */  
35     public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException {  
36         String regExp = "^(5|6|8|9)\\d{7}$";  
37         Pattern p = Pattern.compile(regExp);  
38         Matcher m = p.matcher(str);  
39         return m.matches();  
40     }  
41     
42 }

猜你喜欢

转载自www.cnblogs.com/xuefeilong/p/9160654.html