手机号、邮箱正则验证[通俗易懂]

2022-06-28 08:23:35 浏览数 (1)

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说手机号、邮箱正则验证[通俗易懂],希望能够帮助大家进步!!!

代码语言:javascript复制
private static String MOBILE_REGEX = "^(13[0-9]|15[012356789]|17[3678]|18[0-9]|14[57])[0-9]{8}$";

private static String EMAIL_REGEX = "^\w ((-\w )|(\.\w ))*\@[A-Za-z0-9] ((\.|-)[A-Za-z0-9] )*\.[A-Za-z0-9] $";


/**  * 手机号正则验证  * @param str  * @return  */ public static boolean validateMobile(String str) {
    Pattern pa = Pattern.compile(MOBILE_REGEX);
    Matcher ma = pa.matcher(str);
    while (ma.find()) {
        return true;
    }
    return false;
}

/**  * 邮箱正则验证  * @param str  * @return  */ public static boolean validateEmail(String str) {
    Pattern pa = Pattern.compile(EMAIL_REGEX);
    Matcher ma = pa.matcher(str);
    while (ma.find()) {
        return true;
    }
    return false;
}

0 人点赞