android 手机号码校验

2021-11-15  本文已影响0人  hao_developer

首先定义一个校验手机号码的方法

 public boolean isMobileNO(String mobiles) {
        //"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个
        //"\\d{9}"代表后面是可以是0~9的数字,有9位
        String telRegex = "[1][34578]\\d{9}";
        if (TextUtils.isEmpty(mobiles)) return false;
        else return mobiles.matches(telRegex);
    }

手机号校验的方法之二:

 public static boolean isMobilPhone(String phone) {
        if (StringUtil.isEmpty(phone)) {
            return false;
        }
        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) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }

其次在需要引用的地方进行引用

上一篇 下一篇

猜你喜欢

热点阅读