Java手机号邮箱格式验证类

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Mobile {

    public static void main(String[] args) {
        System.out.println("正确格式的手机号:" + isMobile("13496215263"));
    }


    /**
     * @Description: 手机号格式验证
     * 2019年1月16日已知
     * 中国电信号段
     * 133,149,153,173,174,177,180,181,189,199
     * 中国联通号段
     * 130,131,132,145,146,155,156,166,175,176,185,186
     * 中国移动号段
     * 134(0-8),135,136,137,138,139,147,148,150,151,152,157,158,159,165,178,182,183,184,187,188,198
     * 上网卡专属号段(用于上网和收发短信,不能打电话)
     * 如中国联通的是145
     * 虚拟运营商
     * 电信:1700,1701,1702
     * 移动:1703,1705,1706
     * 联通:1704,1707,1708,1709,171
     * 卫星通信: 1349 <br>     未知号段:141、142、143、144、154
     */
    public static boolean isMobile(String phone) {
        Pattern pattern = null;
        Matcher matcher = null;
        boolean bool = false;
        String str = "^[1](([3|5|8][\\d])|([4][5,6,7,8,9])|([6][5,6])|([7][3,4,5,6,7,8])|([9][8,9]))[\\d]{8}$";
        if (StringUtils.isNotBlank(phone)) {
            pattern = Pattern.compile(str);
            matcher = pattern.matcher(phone);
            bool = matcher.matches();
        }
        return bool;
    }


    /**
     * @Description: 邮箱格式验证
     */
    public static boolean isEmail(String email) {
        Pattern pattern = null;
        Matcher matcher = null;
        boolean bool = false;
        String str = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";
        if (StringUtils.isNotBlank(email)) {
            pattern = Pattern.compile(str);
            matcher = pattern.matcher(email);
            bool = matcher.matches();
        }
        return bool;
    }
}
发布了20 篇原创文章 · 获赞 3 · 访问量 3583

猜你喜欢

转载自blog.csdn.net/bai1964847519/article/details/103983175