Android 利用正则表达式获取检验手机号

1、创建一个Utils类,正则匹配手机号码
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
/**
* 正则匹配手机号码:
*/
public static boolean checkTel(String tel){
Pattern p = Pattern.compile("^[1][3,4,5,7,8,9][0-9]{9}$");
Matcher matcher = p.matcher(tel);
return matcher.matches();
}
}
2、引用类
private EventHandler eh;   //事件处理者
eh = new EventHandler() {};
//注册一个事件回调监听,用于处理SMSSDK接口请求的结果
SMSSDK.registerEventHandler(eh);
phoneNum = editTextPhoneNum.getText().toString();
if(!phoneNum.isEmpty()){
if(Utils.checkTel(phoneNum)){ //利用正则表达式获取检验手机号
// 获取验证码
SMSSDK.getVerificationCode("86", phoneNum);
}else{
Toast.makeText(getApplicationContext(),"请输入有效的手机号",Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(getApplicationContext(),"请输入手机号",Toast.LENGTH_LONG).show();
return;
}
phoneNum = editTextPhoneNum.getText().toString();
 
 
posted @ 2021-07-06 11:25  仙人湖中的鱼  阅读(622)  评论(0编辑  收藏  举报