admin 管理员组

文章数量: 888526

Android 手机号格式验证(正则)

这是一个简易版本的,只验证位数和前两位

//判断输入的格式是否为手机号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;}
}

 

再附上密码格式验证(正则)

常用正则表达式大全

本文标签: Android 手机号格式验证(正则)