利用Java正则表达式校验邮箱与手机号
主要是运用java.util.regex类。
import java.util.regex.matcher;
import java.util.regex.pattern;
public class checkmobileandemail {
/**
* 验证邮箱地址是否正确
* @param email
* @return
*/
public static boolean checkemail(string email){
boolean flag = false;
try{
string check = "^([a-z0-9a-z]+[-|\\.]?)+[a-z0-9a-z]@([a-z0-9a-z]+(-[a-z0-9a-z]+)?\\.)+[a-za-z]{2,}$";
pattern regex = pattern.compile(check);
matcher matcher = regex.matcher(email);
flag = matcher.matches();
}catch(exception e){
flag = false;
}
return flag;
}
/**
* 验证手机号码
* @param mobiles
* @return [0-9]{5,9}
*/
public static boolean ismobileno(string mobiles){
boolean flag = false;
try{
pattern p = pattern.compile("^((13[0-9])|(15[^4,\\d])|(18[0,5-9]))\\d{8}$");
matcher m = p.matcher(mobiles);
flag = m.matches();
}catch(exception e){
flag = false;
}
return flag;
}
public static boolean isnum(string number){
boolean flag = false;
try{
pattern p = pattern.compile("^[0-9]{5}$");
matcher m = p.matcher(number);
flag = m.matches();
}catch(exception e){
flag = false;
}
return flag;
}
}
推荐阅读