1
0

PhoneFormatCheckUtils.java 1.3 KB

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