1、邮箱正则表达式
public boolean isEmail(String strEmail) {
String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher(strEmail);
return m.matches();
}
2、验证手机格式
public static boolean isMobileNO(String mobiles) {
/*
移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
联通:130、131、132、152、155、156、185、186
电信:133、153、180、189、(1349卫通) //177电信4G
总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9
*/
String telRegex = "[1][34578]\\d{9}";//"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。
if (TextUtils.isEmpty(mobiles))
return false;
else
return mobiles.matches(telRegex);
}
3、验证车牌号
public static boolean isCarNo(String carNO) {
String strPattern = "^[A-Z0-9]*[A-Z0-9]";
if (TextUtils.isEmpty(carNO)) {
return false;
} else {
return carNO.matches(strPattern);
}
}
4、判断字符串是否是日期格式
public static boolean isValidDate(String str) {
boolean convertSuccess = true;
// 指定日期格式为四位年-两位月份-两位日期,注意yyyy-MM-dd区分大小写;
SimpleDateFormat simpleDataFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2015-11-29会被接受,并转换成2015-11-01
simpleDataFormat.setLenient(false);
simpleDataFormat.parse(str);
} catch (ParseException e) {
e.printStackTrace();
// 如果throw 就说明格式不对
convertSuccess = false;
}
return convertSuccess;
}