1 packagecom.linbilin.phone;2
3 importjava.util.regex.Matcher;4 importjava.util.regex.Pattern;5
6 public classCheckPhone {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,170013 * **/
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,170918 * **/
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,18424 * ,187,188,147,178,170525 * **/
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 *@authorLinBilin31 *@paramstr32 * 校验电话字符串33 *@return返回true,否则为false34 */
35 public static booleanisPhoneCallNum(String str) {36
37 return str == null || str.trim().equals("") ? false: match(38 PHONE_CALL_PATTERN, str);39 }40
41 /**
42 * 验证【电信】手机号码的格式43 *44 *@authorLinBilin45 *@paramstr46 * 校验手机字符串47 *@return返回true,否则为false48 */
49 public static booleanisChinaTelecomPhoneNum(String str) {50
51 return str == null || str.trim().equals("") ? false: match(52 CHINA_TELECOM_PATTERN, str);53 }54
55 /**
56 * 验证【联通】手机号码的格式57 *58 *@authorLinBilin59 *@paramstr60 * 校验手机字符串61 *@return返回true,否则为false62 */
63 public static booleanisChinaUnicomPhoneNum(String str) {64
65 return str == null || str.trim().equals("") ? false: match(66 CHINA_UNICOM_PATTERN, str);67 }68
69 /**
70 * 验证【移动】手机号码的格式71 *72 *@authorLinBilin73 *@paramstr74 * 校验手机字符串75 *@return返回true,否则为false76 */
77 public static booleanisChinaMobilePhoneNum(String str) {78
79 return str == null || str.trim().equals("") ? false: match(80 CHINA_MOBILE_PATTERN, str);81 }82
83
84 /**
85 * 验证手机和电话号码的格式86 *87 *@authorLinBilin88 *@paramstr89 * 校验手机字符串90 *@return返回true,否则为false91 */
92 public static booleanisPhoneNum(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 *@parampat138 * 表达式139 *@paramstr140 * 待验证字符串141 *@return返回true,否则为false142 */
143 private static booleanmatch(String pat, String str) {144 Pattern pattern =Pattern.compile(pat);145 Matcher match =pattern.matcher(str);146 returnmatch.find();147 }148
149 public static voidmain(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 }