博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

手机号正则校验

Posted on 2021-10-20 23:23  青柠时光  阅读(1898)  评论(0编辑  收藏  举报
public boolean verifyPhone(String phone) {
       String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        if (phone.length() != 11) {
            System.out.println("手机号应为11位数");
        } else {
           Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if (isMatch) {
                log.info("手机号格式正确");
                return true;
            } else {
                log.info("手机号格式错误");
            }
        }
        return false;
    }