各类证件Java正则校验

各类证件Java正则校验
注:字符串为进行小写匹配,使用时可以将字符串进行大写转换(如:营业执照:businessNumber = businessNumber.toUpperCase()😉
若有其他正则,可能会对文档进行更新处理。

import com.shuodao.common.util.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class RegUtil{

    private static final Logger logger = LoggerFactory.getLogger(RegUtil.class);

    /**
     * 判断对象数组中所有的元素为null, 或去除空格符之后长度为0 数组为空,则返回true
     *
     * @param objects
     *            常用类型 Date,String,Integer
     * @return 全部为null返回true
     */
    public static boolean isAllBlank(Object... objects) {
        if (objects == null)
            return true;
        for (int i = 0; i < objects.length; i++) {
            Object object = objects[i];
            if (object != null && !object.toString().trim().isEmpty())
                return false;
        }
        return true;
    }

    /**
     * 判断对象数组中任意一个元素为null, 或去除空格符之后长度为0 数组为空,则返回true
     *
     * @param objects
     *            常用类型 Date,String,Integer
     * @return 任意一个为null返回true
     */
    public static boolean isAnyBlank(Object... objects) {
        if (objects == null)
            return true;
        for (int i = 0; i < objects.length; i++) {
            Object object = objects[i];
            if (object == null || object.toString().trim().isEmpty())
                return true;
        }
        return false;
    }

    /**
     *  匹配全中文,全中文返回true(中文中可包含·,新疆地址姓名可包含·)
     * @param str
     * @return
     */
    public static boolean isRealName(String str) {
        boolean bflag = false;
        if (str==null)
        {
            return bflag;
        }
        char[] c = str.toCharArray();
        for (int i = 0; i <c.length ;i++ )
        {
            if ((c[i] >= 0x4E00 && c[i] <= 0x9FA5) || (i != 0 && i != c.length-1 && c[i] == 183))
            {
                bflag = true;
            }else {
                bflag = false;
                return bflag;
            }
        }
        return bflag;
    }

    /**
     *  校验姓名
     * @Title: isMobile @Description:
     * str @param @return 参数 @return boolean 返回类型 @throws
     */
    public static boolean isName(String str) {
        Pattern pattern = Pattern
                .compile("^[\\u4e00-\\u9fa5]{2,}$");
        Matcher match = pattern.matcher(str);
        if (!match.matches()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     *  校验手机号
     * @Title: isMobile @Description:
     * str @param @return 参数 @return boolean 返回类型 @throws
     */
    public static boolean isMobile(String str) {
        Pattern pattern = Pattern
                .compile("^(0|86|17951)?(13[0-9]|14[57]|15[012356789]|17[013678]|18[0-9]|19[189])[0-9]{8}$");
        Matcher match = pattern.matcher(str);
        if (!match.matches()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     *  校验身份证(包含18位,15位)
     * @param idCard
     * @return
     */
    public static boolean isIdCard(String idCard) {
        Pattern pattern = Pattern
                .compile("((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})" +
                        "(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}" +
                        "[Xx0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))");
        Matcher match = pattern.matcher(idCard);
        if (!match.matches()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     *  身份证,出生日期验证
     * 时间工具类DateTimeUtil就是将字符串时间转为Date类型,不提供
     * @param idCard    身份证号码
     * @param birthday  出生日期
     * @return
     */
    public static boolean idCardAndBirthday(String idCard,String birthday){
        if (isAllBlank(birthday)){
            return true;
        }
        String idCardToBirthday = idCard.substring(6,14);
        Date date = DateTimeUtil.parseDate(birthday);
        int year = date.getYear()+1900;
        int month = date.getMonth()+1;
        int day =date.getDate();
        if (month < 10) birthday = year + "0" + month;
        else birthday = year +""+ month + "";

        if (day < 10) birthday = birthday + "0" + day;
        else birthday = birthday + day;

        if (idCardToBirthday.equals(birthday)){
            return true;
        }
        return false;
    }

    /**
     * 营业执照 统一社会信用代码(15位)
     * @param license
     * @return
     */
    public static boolean isLicense15(String license) {
        if(StringUtils.isEmpty(license)) {
            return false;
        }
        if(license.length() != 15) {
            return false;
        }

        String businesslicensePrex14 = license.substring(0,14);// 获取营业执照注册号前14位数字用来计算校验码
        String businesslicense15 = license.substring(14, license.length());// 获取营业执照号的校验码
        char[] chars = businesslicensePrex14.toCharArray();
        int[] ints = new int[chars.length];
        for(int i=0; i<chars.length;i++) {
            ints[i] = Integer.parseInt(String.valueOf(chars[i]));
        }
        getCheckCode(ints);
        if(businesslicense15.equals(getCheckCode(ints)+"")) {// 比较 填写的营业执照注册号的校验码和计算的校验码是否一致
            return true;
        }
        return false;
    }

    /**
     * 获取 营业执照注册号的校验码
     * @param ints
     * @return
     */
    private static int getCheckCode(int[] ints) {
        if(null != ints && ints.length > 1) {
            int ti = 0;
            int si = 0;// pi|11+ti
            int cj = 0;// (si||10==0?10:si||10)*2
            int pj = 10;// pj=cj|11==0?10:cj|11
            for (int i=0;i<ints.length;i++) {
                ti = ints[i];
                pj = (cj % 11) == 0 ? 10 : (cj % 11);
                si = pj + ti;
                cj = (0 == si % 10 ? 10 : si % 10) * 2;
                if (i == ints.length-1) {
                    pj = (cj % 11) == 0 ? 10 : (cj % 11);
                    return pj == 1 ? 1 : 11 - pj;
                }
            }
        }// end if
        return -1;
    }

    /**
     * 营业执照 统一社会信用代码(18位)
     * @param license
     * @return
     */
    public static boolean isLicense18(String license) {
        Pattern pattern = Pattern
                .compile("^(?:(?![IOZSV])[\\dA-Z]){2}\\d{6}(?:(?![IOZSV])[\\dA-Z]){10}$");
        Matcher match = pattern.matcher(license);
        if (!match.matches()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 校验护照
     * @param passPortCard
     * @return
     */
    public static boolean isPassPortCard(String passPortCard) {
        Pattern pattern = Pattern
                .compile("(^[EeKkGgDdSsPpHh]\\d{8}$)|(^(([Ee][a-fA-F])|([DdSsPp][Ee])|([Kk][Jj])|([Mm][Aa])|(1[45]))\\d{7}$)");
        Matcher match = pattern.matcher(passPortCard);
        if (!match.matches()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 校验港澳居民来往通行证
     * (通行证证件号码共11位。第1位为字母,“H”字头签发给香港居民,“M”字头签发给澳门居民,第2位至第11位为数字)
     * @param passCard
     * @return
     */
    public static boolean isPassCard(String passCard) {
        Pattern pattern = Pattern
                .compile("^([H|M]\\d{10})$");
        Matcher match = pattern.matcher(passCard);
        if (!match.matches()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 校验驾照(驾照与身份证一般都是相同的,都是15位,18位区分)
     * @param drivingLicense
     * @return
     */
    public static boolean isDrivingLicense(String drivingLicense) {
        Pattern pattern = Pattern
                .compile("((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})" +
                        "(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}" +
                        "[Xx0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))");
        Matcher match = pattern.matcher(drivingLicense);
        if (!match.matches()) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 模糊电话号码
     * @param mobile
     * @return
     */
    public static String mobileReplace(String mobile) {
        if(StringUtils.isBlank(mobile) || mobile.length()<8) {
            return mobile;
        }
        StringBuilder sb = new StringBuilder(mobile);
        sb.replace(mobile.length()-8, mobile.length()-4, "****");
        return sb.toString();
    }

    /**
     * 模糊身份证
     * @param idCard
     * @return
     */
    public static String idCardReplace(String idCard) {
        if(StringUtils.isBlank(idCard)) {
            return idCard;
        }
        StringBuilder sb = new StringBuilder(idCard);
        sb.replace(idCard.length()-12, idCard.length()-2, "********");
        return sb.toString();
    }

    /**
     * 模糊姓名
     * @param realName
     * @return
     */
    public static String realNameReplace(String realName) {
        if(StringUtils.isBlank(realName)) {
            return realName;
        }
        StringBuilder sb = new StringBuilder(realName);
        for (int i = 1; i < sb.length(); i++){
            if (sb.length() > 2 && i == sb.length()-1) continue;
            sb.replace(i, i+1, "*");
        }
        //去除多个*,只保留一个
        int i;
        while ((i = 2) < sb.length()-1){
            sb.deleteCharAt(i);
        }
        return sb.toString();
    }

    /**
     * 模糊营业执照号
     * @param businessNumber
     * @return
     */
    public static String businessNumberReplace(String businessNumber) {
        if(StringUtils.isBlank(businessNumber) || businessNumber.length()<8) {
            return businessNumber;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(businessNumber.substring(0,3));
        sb.append("****");
        sb.append(businessNumber.substring(businessNumber.length()-3));
        return sb.toString();
    }

}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值