All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lodsve.core.utils.ValidateUtils Maven / Gradle / Ivy

There is a newer version: 2.7.5-RELEASE
Show newest version
/*
 * Copyright (C) 2018  Sun.Hao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package lodsve.core.utils;

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

/**
 * 验证工具栏
 *
 * @author sunhao([email protected])
 * @version V1.0
 * @createTime 2012-7-12 下午04:09:14
 */
public class ValidateUtils {

    /**
     * 邮箱正则表达式
     */
    private static final String REG_EMAIL = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
    /**
     * 固话正则表达式
     */
    private static final String REG_PHONE = "\\d{3}-\\d{8}|\\d{4}-\\d{7}";
    /**
     * 手机号码正则表达式
     */
    private static final String REG_MOBILE = "0{0,1}(13[4-9]|15[7-9]|15[0-2]|18[7-9])[0-9]{8}";
    /**
     * url正则表达式
     */
    private static final String REG_URL = "^((https|http|ftp|rtsp|mms)://)?[A-Za-z0-9]+\\.[A-Za-z0-9]+[\\/=\\?%\\-&_~`@\\':+!]*([^<>\\\"\\\"])*$";
    /**
     * 身份证号码正则表达式
     */
    private static final String REG_IDCARD = "\\d{15}|\\d{18}";
    /**
     * 是否是数字的正则表达式
     */
    private static final String REG_NUMBER = "\\d+";
    /**
     * 邮编的正则表达式
     */
    private static final String REG_ZIP = "^[1-9]\\d{5}$";
    /**
     * QQ
     */
    private static final String REG_QQ = "[1-9]\\d{4,13}";
    /**
     * 整数
     */
    private static final String REG_INTEGER = "[-\\+]?\\d+";
    /**
     * 小数
     */
    private static final String REG_DOUBLE = "[-\\+]?\\d+(\\.\\d+)?";
    /**
     * 英文
     */
    private static final String REG_ENGLISH = "^[A-Za-z]+$";
    /**
     * 中文
     */
    private static final String REG_CHINESE = "^[\\u0391-\\uFFE5]+$";
    /**
     * IP
     */
    private static final String REG_IP = "(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])";

    /**
     * 私有化构造器
     */
    private ValidateUtils(){}

    /**
     * 判断是否是邮箱地址
     *
     * @param email        待验证的邮箱地址
     * @return
     */
    public static boolean isEmail(String email) {
        //不区分大小写
        Pattern pattern = Pattern.compile(REG_EMAIL, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(email);

        /**
         * 注意find与matches()的区别
         * 调用find方法,只执行尽量匹配
         * 而调用matches方法,则执行严格的匹配
         */
        return StringUtils.isNotEmpty(email) && matcher.matches();
    }

    /**
     * 判断是否是电话号码,电话(固话)
     *
     * @param phone 固话号码
     * @return
     */
    public static boolean isPhone(String phone) {
        Pattern patter = Pattern.compile(REG_PHONE, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(phone);

        return StringUtils.isNotEmpty(phone) && matcher.matches();
    }

    /**
     * 判断是否是手机号码
     *
     * @param mobile 手机号码
     * @return
     */
    public static boolean isMobile(String mobile) {
        Pattern patter = Pattern.compile(REG_MOBILE, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(mobile);

        return StringUtils.isNotEmpty(mobile) && matcher.matches();
    }

    /**
     * 判断是否是正确的url
     *
     * @param url
     * @return
     */
    public static boolean isUrl(String url) {
        Pattern patter = Pattern.compile(REG_URL, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(url);

        return StringUtils.isNotEmpty(url) && matcher.matches();
    }

    /**
     * 判断是否是合法的身份证号码
     *
     * @param idCard 身份证号码
     * @return
     */
    public static boolean isIdCard(String idCard) {
        Pattern patter = Pattern.compile(REG_IDCARD, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(idCard);

        return StringUtils.isNotEmpty(idCard) && matcher.matches();
    }

    /**
     * 判断是否是数字
     *
     * @param number 数字
     * @return
     */
    public static boolean isNumber(String number) {
        Pattern patter = Pattern.compile(REG_NUMBER, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(number);

        return StringUtils.isNotEmpty(number) && matcher.matches();
    }

    /**
     * 判断邮编是否合法
     *
     * @param zip 邮编
     * @return
     */
    public static boolean isZip(String zip) {
        Pattern patter = Pattern.compile(REG_ZIP, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(zip);

        return StringUtils.isNotEmpty(zip) && matcher.matches();
    }

    /**
     * 判断QQ号是否合法
     *
     * @param qq
     * @return
     */
    public static boolean isQq(String qq) {
        Pattern patter = Pattern.compile(REG_QQ, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(qq);

        return StringUtils.isNotEmpty(qq) && matcher.matches();
    }

    /**
     * 判断是否是整数
     *
     * @param integer
     * @return
     */
    public static boolean isInteger(String integer) {
        Pattern patter = Pattern.compile(REG_INTEGER, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(integer);

        return StringUtils.isNotEmpty(integer) && matcher.matches();
    }

    /**
     * 判断是否是小数
     *
     * @param param
     * @return
     */
    public static boolean isDouble(String param) {
        Pattern patter = Pattern.compile(REG_DOUBLE, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(param);

        return StringUtils.isNotEmpty(param) && matcher.matches();
    }

    /**
     * 判断是否是英文
     *
     * @param english
     * @return
     */
    public static boolean isEnglish(String english) {
        Pattern patter = Pattern.compile(REG_ENGLISH, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(english);

        return StringUtils.isNotEmpty(english) && matcher.matches();
    }

    /**
     * 判断QQ号是否合法
     *
     * @param chinese
     * @return
     */
    public static boolean isChinese(String chinese) {
        Pattern patter = Pattern.compile(REG_CHINESE, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(chinese);

        return StringUtils.isNotEmpty(chinese) && matcher.matches();
    }

    /**
     * 判断IP是否合法
     *
     * @param ip
     * @return
     */
    public static boolean isIp(String ip) {
        Pattern patter = Pattern.compile(REG_IP, Pattern.CASE_INSENSITIVE);
        Matcher matcher = patter.matcher(ip);

        return StringUtils.isNotEmpty(ip) && matcher.matches();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy