查看: 491|回复: 0

[Java代码] java 验证手机号码、电话号码(包括最新的电信、联通和移动号码)

发表于 2017-8-7 08:00:02
目前的号码段(2017-06-01更新)
java代码
  1. package com.linbilin.test;
  2. import java.util.regex.Pattern;
  3. public class CheckPhone {
  4. /** 座机电话格式验证 **/
  5. private static final String PHONE_CALL_PATTERN = "^(?:\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}(?:-\\d{1,4})?$";
  6. /**
  7. * 中国电信号码格式验证 手机段: 133,153,180,181,189,177,1700,173
  8. * **/
  9. private static final String CHINA_TELECOM_PATTERN = "(?:^(?:\\+86)?1(?:33|53|7[37]|8[019])\\d{8}$)|(?:^(?:\\+86)?1700\\d{7}$)";
  10. /**
  11. * 中国联通号码格式验证 手机段:130,131,132,155,156,185,186,145,176,1707,1708,1709,175
  12. * **/
  13. private static final String CHINA_UNICOM_PATTERN = "(?:^(?:\\+86)?1(?:3[0-2]|4[5]|5[56]|7[56]|8[56])\\d{8}$)|(?:^(?:\\+86)?170[7-9]\\d{7}$)";
  14. /**
  15. * 简单手机号码校验,校验手机号码的长度和1开头
  16. */
  17. private static final String SIMPLE_PHONE_CHECK = "^(?:\\+86)?1\\d{10}$";
  18. /**
  19. * 中国移动号码格式验证
  20. * 手机段:134,135,136,137,138,139,150,151,152,157,158,159,182,183,184
  21. * ,187,188,147,178,1705
  22. *
  23. **/
  24. private static final String CHINA_MOBILE_PATTERN = "(?:^(?:\\+86)?1(?:3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(?:^(?:\\+86)?1705\\d{7}$)";
  25. /**
  26. * 仅手机号格式校验
  27. */
  28. private static final String PHONE_PATTERN = new StringBuilder(300)
  29. .append(CHINA_MOBILE_PATTERN).append("|")
  30. .append(CHINA_TELECOM_PATTERN).append("|")
  31. .append(CHINA_UNICOM_PATTERN).toString();
  32. /**
  33. * 手机和座机号格式校验
  34. */
  35. private static final String PHONE_TEL_PATTERN = new StringBuilder(350)
  36. .append(PHONE_PATTERN).append("|").append("(")
  37. .append(PHONE_CALL_PATTERN).append(")").toString();
  38. /**
  39. * 匹配多个号码以,、或空格隔开的格式,如 17750581369
  40. * 13306061248、(596)3370653,17750581369,13306061248 (0596)3370653
  41. * @param input
  42. * @param separator 可以自己指定分隔符,如"、, "表示可以以顿号、逗号和空格分隔
  43. * @return
  44. */
  45. public static boolean checkMultiPhone(String input, String separator) {
  46. separator = escapeMetacharacterOfStr(separator);
  47. String regex = "^(?!.+["
  48. + separator
  49. + "]$)(?:(?:(?:(?:\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}(?:-\\d{1,4})?)|(?:1\\d{10}))(?:["
  50. + separator + "]|$))+$";
  51. return match(regex, input);
  52. }
  53. /**
  54. * 转义字符串中的[]-^\元字符
  55. *
  56. * @param input
  57. * @param separator
  58. * @return
  59. */
  60. private static String escapeMetacharacterOfStr(String input) {
  61. String regex = "[-^\\[\\]\\\\]";
  62. return input.replaceAll(regex, "\\\\$0");
  63. }
  64. /**
  65. * 仅手机号码校验
  66. *
  67. * @param input
  68. * @return
  69. */
  70. public static boolean isPhone(String input) {
  71. return match(PHONE_PATTERN, input);
  72. }
  73. /**
  74. * 手机号或座机号校验
  75. *
  76. * @param input
  77. * @return
  78. */
  79. public static boolean isPhoneOrTel(String input) {
  80. System.out.println(PHONE_TEL_PATTERN);
  81. return match(PHONE_TEL_PATTERN, input);
  82. }
  83. /**
  84. * 验证电话号码的格式
  85. *
  86. * @author LinBilin
  87. * @param str
  88. * 校验电话字符串
  89. * @return 返回true,否则为false
  90. */
  91. public static boolean isPhoneCallNum(String str) {
  92. return match(PHONE_CALL_PATTERN, str);
  93. }
  94. /**
  95. * 验证【电信】手机号码的格式
  96. *
  97. * @author LinBilin
  98. * @param str
  99. * 校验手机字符串
  100. * @return 返回true,否则为false
  101. */
  102. public static boolean isChinaTelecomPhoneNum(String str) {
  103. return match(CHINA_TELECOM_PATTERN, str);
  104. }
  105. /**
  106. * 验证【联通】手机号码的格式
  107. *
  108. * @author LinBilin
  109. * @param str
  110. * 校验手机字符串
  111. * @return 返回true,否则为false
  112. */
  113. public static boolean isChinaUnicomPhoneNum(String str) {
  114. return match(CHINA_UNICOM_PATTERN, str);
  115. }
  116. /**
  117. * 验证【移动】手机号码的格式
  118. *
  119. * @author LinBilin
  120. * @param str
  121. * 校验手机字符串
  122. * @return 返回true,否则为false
  123. */
  124. public static boolean isChinaMobilePhoneNum(String str) {
  125. return match(CHINA_MOBILE_PATTERN, str);
  126. }
  127. /**
  128. * 简单手机号码校验,校验手机号码的长度和1开头
  129. *
  130. * @param str
  131. * @return
  132. */
  133. public static boolean isPhoneSimple(String str) {
  134. return match(SIMPLE_PHONE_CHECK, str);
  135. }
  136. /**
  137. * 匹配函数
  138. *
  139. * @param regex
  140. * @param input
  141. * @return
  142. */
  143. private static boolean match(String regex, String input) {
  144. return Pattern.matches(regex, input);
  145. }
  146. }
复制代码


回复

使用道具 举报