PhoneUtils.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package com.ruoyi.common.utils;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class PhoneUtils {
  5. /**
  6. * 判断是不是合法的手机号码
  7. * @param phoneNumber
  8. * @return
  9. */
  10. public static boolean isPhoneNumber(String phoneNumber){
  11. if(StringUtils.isEmpty(phoneNumber)){
  12. return false;
  13. }
  14. //第一位是不是0
  15. String phoneOne=phoneNumber.substring(0,1);
  16. //是不是 +86形式
  17. int is86=phoneNumber.indexOf("+86");
  18. if(is86==0){
  19. phoneNumber = phoneNumber.substring(3,phoneNumber.length());
  20. }
  21. //手机号码长度
  22. int phoneLength=phoneNumber.length();
  23. //是纯数字 并且长度等于11 并且第一位不是0 并且 不包含+86
  24. return isNumeric(phoneNumber)&&phoneLength==11&&!phoneOne.equals("0");
  25. }
  26. /**
  27. * 判断字符串是不是纯数字
  28. */
  29. public static boolean isNumeric(String str){
  30. Pattern pattern = Pattern.compile("[0-9]*");
  31. Matcher isNum = pattern.matcher(str);
  32. if( !isNum.matches() ){
  33. return false;
  34. }
  35. return true;
  36. }
  37. public static void main(String[] args) {
  38. System.out.println(new PhoneUtils().isPhoneNumber("18500040805"));
  39. }
  40. }