12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package com.ruoyi.common.utils;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class PhoneUtils {
- /**
- * 判断是不是合法的手机号码
- * @param phoneNumber
- * @return
- */
- public static boolean isPhoneNumber(String phoneNumber){
- if(StringUtils.isEmpty(phoneNumber)){
- return false;
- }
- //第一位是不是0
- String phoneOne=phoneNumber.substring(0,1);
- //是不是 +86形式
- int is86=phoneNumber.indexOf("+86");
- if(is86==0){
- phoneNumber = phoneNumber.substring(3,phoneNumber.length());
- }
- //手机号码长度
- int phoneLength=phoneNumber.length();
- //是纯数字 并且长度等于11 并且第一位不是0 并且 不包含+86
- return isNumeric(phoneNumber)&&phoneLength==11&&!phoneOne.equals("0");
- }
- /**
- * 判断字符串是不是纯数字
- */
- public static boolean isNumeric(String str){
- Pattern pattern = Pattern.compile("[0-9]*");
- Matcher isNum = pattern.matcher(str);
- if( !isNum.matches() ){
- return false;
- }
- return true;
- }
- public static void main(String[] args) {
- System.out.println(new PhoneUtils().isPhoneNumber("18500040805"));
- }
- }
|