常用正则校验

public class RegexUtils {
    /**
     * 手机号正则匹配
     * 
     * @param mobileNumber
     *            手机号
     * @return true:手机号格式正确
     */
    public static final boolean mobileRegex(String mobileNumber) {
        // 非空判断
        if (null == mobileNumber)
            return false;

        Pattern p = Pattern.compile("^1[34578]\\d{9}$");
        Matcher m = p.matcher(mobileNumber);
        return m.matches();
    }
    /**
     * 简单手机号正则匹配(11位数字)
     * 
     * @param mobile
     *            手机号
     * @return true:手机号格式正确
     */
    public static final boolean simpleMobileRegex(String mobile) {
        // 非空判断
        if (null == mobile)
            return false;
        
        Pattern p = Pattern.compile("^[0-9]\\d{10}$");
        Matcher m = p.matcher(mobile);
        return m.matches();
    }

    /**
     * 邮箱地址正则匹配
     * 
     * @param email
     *            邮箱地址
     * @return true:邮箱地址格式正确
     */
    public static final boolean emailRegex(String email) {
        // 非空判断
        if (null == email)
            return false;

        Pattern p = Pattern
                .compile("^\\w+@[a-z0-9A-Z]+\\.[a-z]+$");
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 身份证号码正则匹配
     * 
     * @param idCardNo
     *            身份证号码
     * @return true:身份证号码格式正确
     */
    public static final boolean idCardNoRegex(String idCardNo) {
        // 非空判断
        if (null == idCardNo)
            return false;

        Pattern p = Pattern.compile("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)");
        
        Matcher m = p.matcher(idCardNo);
        return m.matches();
    }
    /**
     * 简单银行卡号正则匹配(六至十九位数字)
     * 
     * @param bankCardNo
     *            银行卡号
     * @return true:银行卡号
     */
    public static final boolean bankCardNoRegex(String bankCardNo) {
        // 非空判断
        if (null == bankCardNo)
            return false;
        
        Pattern p = Pattern.compile("(^\\d{6,19}$)");
        Matcher m = p.matcher(bankCardNo);
        return m.matches();
    }
    /**
     * 密码复杂度正则
     * 
     * @param pwd
     * @return true:符合要求
     */
    public static final boolean pwdComplexRegex(String pwd) {
        if (StringUtils.isBlank(pwd))
            return false;
        // 密码长度校验
        int pwdLen = pwd.length();
        if (pwdLen < 6 || pwdLen > 20)
            return false;
        // 6-20位数字,字母或符号
        Pattern p = Pattern.compile("^.{6,20}$");
        Matcher m = p.matcher(pwd);
        return m.matches();
    }

    
    
    /**
     * 身份证格式匹配
     * 
     * @param identityCardNo
     * @return true:居民身份证格式正确
     */
    public static final String identityCardNoRegex(String cardType, String identityCardNo) {
        String message = null;

        // 居民身份证
        if (CardType.ID_CARD.getIndex().equals(cardType)) {
            Pattern p = Pattern.compile("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)");
            Matcher m = p.matcher(identityCardNo);
            if (!m.matches()) {
                message = "居民身份证格式错误!";
                return message;
            }
        }
        // 护照
        if (CardType.PASSPORT.getIndex().equals(cardType)) {
            Pattern p = Pattern.compile("^[a-zA-Z]{5,17}$|^[a-zA-Z0-9]{5,17}$");
            Matcher m = p.matcher(identityCardNo);
            if (!m.matches()) {
                message = "护照格式错误!";
                return message;
            }
        }
        // 工商注册号码
        if (CardType.BUSINESS_REGISTRATION_CODE.getIndex().equals(cardType)) {
            Pattern p = Pattern.compile("[^_IOZSVa-z\\W]{2}\\d{6}[^_IOZSVa-z\\W]{10}$");
            Matcher m = p.matcher(identityCardNo);
            if (!m.matches()) {
                message = "工商注册号码格式错误!";
                return message;
            }
        }
        // 组织机构代码
        if (CardType.ORGANIZATION_CODE.getIndex().equals(cardType)) {
            Pattern p = Pattern.compile("[a-zA-Z0-9]{8}-[a-zA-Z0-9]");
            Matcher m = p.matcher(identityCardNo);
            if (!m.matches()) {
                message = "组织机构代码格式错误!";
                return message;
            }
        }
        // 军官证
        if (CardType.MILITARY_CARD.getIndex().equals(cardType)) {
            Pattern p = Pattern.compile("\\d{6,8}$");
            Matcher m = p.matcher(identityCardNo);
            if (!m.matches()) {
                message = "军人证格式错误(6到8位数字)!";
                return message;
            }
        }
        return message;

    }

    /**
     * 实名姓名正则
     * 
     * @param realName
     * @return true:真实姓名吻合
     */
    public static final boolean realNameRegex(String realName) {
        if (StringUtils.isBlank(realName))
            return false;
        Pattern p = Pattern.compile("^(([\u4E00-\u9FA5]{2,50})|([a-zA-Z]{3,50}))$");
        Matcher m = p.matcher(realName);
        return m.matches();
    }
    
    /**
     * 图片类型正则
     * 
     * @param 图片类型
     * @return true:图片类型吻合
     */
    public static final boolean fileTypeRegex(String fileType) {
        Pattern p = Pattern.compile("^(png|jpg|bmp|gif|PNG|JPG|BMP|GIF)$");
        Matcher m = p.matcher(fileType);
        return m.matches();
    }
    /**
     * 电话QQ正则
     * 
     * @param 电话QQ
     * @return true:电话QQ吻合
     */
    public static final boolean QQMobilePhoneRegex(String number) {
        Pattern p = Pattern.compile("^[0-9]*$");
        Matcher m = p.matcher(number);
        return m.matches();
    }
    
    
}

猜你喜欢

转载自blog.csdn.net/sunkwa/article/details/84787914
今日推荐