MobileUtil.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.dtb.portal.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.lang3.StringUtils;
  4. import java.util.regex.Pattern;
  5. /**
  6. * @author fuzhiyou
  7. * @date 2022-04-14 16:02
  8. * <p>
  9. * 验证手机号工具类
  10. */
  11. @Slf4j
  12. public class MobileUtil {
  13. /**
  14. * 中国电信号码格式验证 手机段: 133,149,153,173,177,180,181,189,199,1349,1410,1700,1701,1702
  15. **/
  16. private static final String CHINA_TELECOM_PATTERN = "(?:^(?:\\+86)?1(?:33|49|53|7[37]|8[019]|99)\\d{8}$)|(?:^(?:\\+86)?1349\\d{7}$)|(?:^(?:\\+86)?1410\\d{7}$)|(?:^(?:\\+86)?170[0-2]\\d{7}$)";
  17. /**
  18. * 中国联通号码格式验证 手机段:130,131,132,145,146,155,156,166,171,175,176,185,186,1704,1707,1708,1709
  19. **/
  20. private static final String CHINA_UNICOM_PATTERN = "(?:^(?:\\+86)?1(?:3[0-2]|4[56]|5[56]|66|7[156]|8[56])\\d{8}$)|(?:^(?:\\+86)?170[47-9]\\d{7}$)";
  21. /**
  22. * 中国移动号码格式验证
  23. * 手机段:134,135,136,137,138,139,147,148,150,151,152,157,158,159,178,182,183,184,187,188,195,198,1440,1703,1705,1706
  24. **/
  25. private static final String CHINA_MOBILE_PATTERN = "(?:^(?:\\+86)?1(?:3[4-9]|4[78]|5[0-27-9]|78|8[2-478]|98|95)\\d{8}$)|(?:^(?:\\+86)?1440\\d{7}$)|(?:^(?:\\+86)?170[356]\\d{7}$)";
  26. /**
  27. * 中国大陆手机号码校验
  28. *
  29. * @param phone
  30. * @return
  31. */
  32. public static boolean checkPhone(String phone) {
  33. if (StringUtils.isNotBlank(phone)) {
  34. if (checkChinaMobile(phone) || checkChinaUnicom(phone) || checkChinaTelecom(phone)) {
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40. /**
  41. * 中国移动手机号码校验
  42. *
  43. * @param phone
  44. * @return
  45. */
  46. public static boolean checkChinaMobile(String phone) {
  47. if (StringUtils.isNotBlank(phone)) {
  48. Pattern regexp = Pattern.compile(CHINA_MOBILE_PATTERN);
  49. if (regexp.matcher(phone).matches()) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. /**
  56. * 中国联通手机号码校验
  57. *
  58. * @param phone
  59. * @return
  60. */
  61. public static boolean checkChinaUnicom(String phone) {
  62. if (StringUtils.isNotBlank(phone)) {
  63. Pattern regexp = Pattern.compile(CHINA_UNICOM_PATTERN);
  64. if (regexp.matcher(phone).matches()) {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. /**
  71. * 中国电信手机号码校验
  72. *
  73. * @param phone
  74. * @return
  75. */
  76. public static boolean checkChinaTelecom(String phone) {
  77. if (StringUtils.isNotBlank(phone)) {
  78. Pattern regexp = Pattern.compile(CHINA_TELECOM_PATTERN);
  79. if (regexp.matcher(phone).matches()) {
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. /**
  86. * 隐藏手机号中间四位
  87. *
  88. * @param phone
  89. * @return java.lang.String
  90. */
  91. public static String hideMiddleMobile(String phone) {
  92. if (StringUtils.isNotBlank(phone)) {
  93. phone = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
  94. }
  95. return phone;
  96. }
  97. }