1 package com.linbilin.phone; 2
3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5
6 public class CheckPhone { 7
8 /** 電話格式驗證 **/
9 private static final String PHONE_CALL_PATTERN = "^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}(-\\d{1,4})?$"; 10
11 /**
12 * 中國電信號碼格式驗證 手機段: 133,153,180,181,189,177,1700 13 * **/
14 private static final String CHINA_TELECOM_PATTERN = "(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)"; 15
16 /**
17 * 中國聯通號碼格式驗證 手機段:130,131,132,155,156,185,186,145,176,1709 18 * **/
19 private static final String CHINA_UNICOM_PATTERN = "(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)"; 20
21 /**
22 * 中國移動號碼格式驗證 23 * 手機段:134,135,136,137,138,139,150,151,152,157,158,159,182,183,184 24 * ,187,188,147,178,1705 25 * **/
26 private static final String CHINA_MOBILE_PATTERN = "(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)"; 27 /**
28 * 驗證電話號碼的格式 29 * 30 * @author LinBilin 31 * @param str 32 * 校驗電話字符串 33 * @return 返回true,否則為false 34 */
35 public static boolean isPhoneCallNum(String str) { 36
37 return str == null || str.trim().equals("") ? false : match( 38 PHONE_CALL_PATTERN, str); 39 } 40
41 /**
42 * 驗證【電信】手機號碼的格式 43 * 44 * @author LinBilin 45 * @param str 46 * 校驗手機字符串 47 * @return 返回true,否則為false 48 */
49 public static boolean isChinaTelecomPhoneNum(String str) { 50
51 return str == null || str.trim().equals("") ? false : match( 52 CHINA_TELECOM_PATTERN, str); 53 } 54
55 /**
56 * 驗證【聯通】手機號碼的格式 57 * 58 * @author LinBilin 59 * @param str 60 * 校驗手機字符串 61 * @return 返回true,否則為false 62 */
63 public static boolean isChinaUnicomPhoneNum(String str) { 64
65 return str == null || str.trim().equals("") ? false : match( 66 CHINA_UNICOM_PATTERN, str); 67 } 68
69 /**
70 * 驗證【移動】手機號碼的格式 71 * 72 * @author LinBilin 73 * @param str 74 * 校驗手機字符串 75 * @return 返回true,否則為false 76 */
77 public static boolean isChinaMobilePhoneNum(String str) { 78
79 return str == null || str.trim().equals("") ? false : match( 80 CHINA_MOBILE_PATTERN, str); 81 } 82
83
84 /**
85 * 驗證手機和電話號碼的格式 86 * 87 * @author LinBilin 88 * @param str 89 * 校驗手機字符串 90 * @return 返回true,否則為false 91 */
92 public static boolean isPhoneNum(String str) { 93 // 如果字符串為空,直接返回false
94 if (str == null || str.trim().equals("")) { 95 return false; 96 } else { 97 int comma = str.indexOf(",");// 是否含有逗號
98 int caesuraSign = str.indexOf("、");// 是否含有頓號
99 int space = str.trim().indexOf(" ");// 是否含有空格
100 if (comma == -1 && caesuraSign == -1 && space == -1) { 101 // 如果號碼不含分隔符,直接驗證
102 str=str.trim(); 103 return (isPhoneCallNum(str) || isChinaTelecomPhoneNum(str) 104 || isChinaUnicomPhoneNum(str) || isChinaMobilePhoneNum(str)) ? true
105 : false; 106 } else { 107 // 號碼含分隔符,先把分隔符統一處理為英文狀態下的逗號
108 if (caesuraSign != -1) { 109 str=str.replaceAll("、", ","); 110 } 111 if (space != -1) { 112 str=str.replaceAll(" ", ","); 113 } 114
115 String[] phoneNumArr = str.split(","); 116 //遍歷驗證
117 for (String temp : phoneNumArr) { 118 temp=temp.trim(); 119 if (isPhoneCallNum(temp) || isChinaTelecomPhoneNum(temp) 120 || isChinaUnicomPhoneNum(temp) 121 || isChinaMobilePhoneNum(temp)) { 122 continue; 123 } else { 124 return false; 125 } 126 } 127 return true; 128 } 129
130 } 131
132 } 133
134 /**
135 * 執行正則表達式 136 * 137 * @param pat 138 * 表達式 139 * @param str 140 * 待驗證字符串 141 * @return 返回true,否則為false 142 */
143 private static boolean match(String pat, String str) { 144 Pattern pattern = Pattern.compile(pat); 145 Matcher match = pattern.matcher(str); 146 return match.find(); 147 } 148
149 public static void main(String[] args) { 150
151 System.out.println(isPhoneNum("17750581369")); 152 System.out.println(isPhoneNum("13306061248")); 153 System.out.println(isPhoneNum("17750581369,13306061248")); 154 System.out.println(isPhoneNum("17750581369 13306061248")); 155 System.out.println(isPhoneNum("17750581369、13306061248")); 156 System.out.println(isPhoneNum("0596-3370653")); 157
158 } 159
160 }