Java验证手机号

威哥不废话,直接上代码:

1.判断方法:

/**
 * 判断是否手机号
 * Author:William(徐威)
 * Create Time:2018-10-05
 * @param phone
 * @return
 */
public static boolean isMobilePhone(String phone) {
    boolean flag=true;
    String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
    if (phone.length() != 11) {
        flag= false;
    } else {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phone);
        flag = m.matches();
    }

    return flag;
}

2.如何使用:

strPhone = etxt_ProductPhoneLogin_Code.getText().toString().trim();
if (strPhone != null && strPhone.length() > 0) {
    if(!StringPlus.isMobilePhone(strPhone)){
        MessageBox.init(getActivity()).showInfoMsg("请输入正确格式的手机号。");
        return;
    }
    //登录按钮
    if (listener != null) {
        listener.onLoginPhone(strPhone);
    }
    etxt_ProductPhoneLogin_Code.setText("");
    dismiss();

3.效果图:

猜你喜欢

转载自blog.csdn.net/xuwei_net/article/details/82973090