这是一个简易版本的,只验证位数和前两位
//判断输入的格式是否为手机号
public boolean isPhone(String phone){
String regex="^1[3456789]\\d{9}$";
if (phone.length()!=11){
Log.i(TAG, "isPhone: 手机位数不对");
return false;
}else {
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(phone);
boolean isMatch=m.matches();
Log.i(TAG, "isPhone: 是否正则匹配"+isMatch);
return isMatch;
}
}
还有一个详细版本
public static boolean isPhone(String phone) {
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) {
MToast.showToast("手机号应为11位数");
return false;
} else {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(phone);
boolean isMatch = m.matches();
Log.i(TAG, "isPhone: 是否正则匹配"+isMatch);
return isMatch;
}
}
判断
if(!isPhone(et_phone.getText().toString())){
Toast.makeText(RegisterActivity.this,"手机号码格式不正确",Toast.LENGTH_SHORT).show();
}else{
//这里写验证码倒计时操作
Toast.makeText(RegisterActivity.this,"发送验证码成功",Toast.LENGTH_SHORT).show();
}
再附上密码格式验证(正则)
https://blog.youkuaiyun.com/YTYT5200/article/details/111994190