Java基础--验证手机号

验证手机号

0:验证通过
1:为空
2:手机号位数不正确
3:验证失败

public class CheckPhoneUtils {
    public static  int  checkPhone(String phone) {
        int status =0;
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        if (StringUtils.isEmpty(phone)) {
            status =  1;
            throw new BusinessException(1,"手机号为空");
        } else {
            if (phone.length() != 11) {
                System.out.println("手机号应为11位数");
                status =  2;
                throw new BusinessException(1,"手机号应为11位数");
            } else {
                Pattern p = Pattern.compile(regex);
                Matcher m = p.matcher(phone);
                boolean isMatch = m.matches();
                if (isMatch) {
                    System.out.println("您的手机号" + phone + "是正确格式@——@");
                } else {
                    System.out.println("您的手机号" + phone + "是错误格式!!!");
                    status =  3;
                    throw new BusinessException(1,"手机号验证失败,请填写正确的手机号");
                }
            }
        }
        return status;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容