Java后台校验手机号和邮箱

时间:2024-02-29 14:41:31
//true是手机号,false不是手机号
public static boolean checkPhone(String phone){
    Pattern p = Pattern.compile("^[1](([3-9][\\d])|([4][5,6,7,8,9])|([6][5,6])|([7][3,4,5,6,7,8])|([9][8,9]))[\\d]{8}$");
    if(p.matcher(phone).matches()){
        return true;
    }
    return false;
}


public static boolean checkEmail(String email) {
    if (StringUtils.isEmpty(email)){
        return false;
    }
    String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    Pattern p;
    Matcher m;
    p = Pattern.compile(regEx1);
    m = p.matcher(email);
    if (m.matches()){
        return true;
    }
    return false;
}