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

com.feilong.validator.ValidatorUtil Maven / Gradle / Ivy

Go to download

feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.

There is a newer version: 4.0.8
Show newest version
/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.validator;

import static com.feilong.core.Validator.isNullOrEmpty;
import static com.feilong.validator.RegexPattern.MOBILEPHONE;
import static com.feilong.validator.RegexPattern.TELEPHONE;
import static com.feilong.validator.RegexPattern.TELEPHONE_MUST_AREACODE;
import static com.feilong.validator.RegexPattern.ZIPCODE;

import com.feilong.core.util.RegexUtil;
import com.feilong.core.Validate;
import com.feilong.lib.validator.EmailValidator;

/**
 * 校验的工具类.
 *
 * @author feilong
 * @since 1.10.6
 */
public final class ValidatorUtil{

    /** Don't let anyone instantiate this class. */
    private ValidatorUtil(){
        //AssertionError不是必须的. 但它可以避免不小心在类的内部调用构造器. 保证该类在任何情况下都不会被实例化.
        //see 《Effective Java》 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    //---------------------------------------------------------------

    /**
     * 判断字符串是否是大陆的电话号码.
     *
     * @param telephone
     *            the telephone
     * @return 如果 telephone 是null或者empty,返回 false
*/ public static boolean isTelephone(String telephone){ return isMatches(TELEPHONE, telephone); } /** * 判断字符串是否是大陆的电话号码(必须要有区号). * * @param telephoneMustAreacode * the telephone must areacode * @return 如果 telephoneMustAreacode 是null或者empty,返回 false
*/ public static boolean isTelephoneMustAreacode(String telephoneMustAreacode){ return isMatches(TELEPHONE_MUST_AREACODE, telephoneMustAreacode); } //--------------------------------------------------------------- /** * 判断字符串是否是邮编. * * @param zipcode * 邮政编码 * @return 如果 zipcode 是null或者empty,返回 false
*/ public static boolean isZipcode(String zipcode){ return isMatches(ZIPCODE, zipcode); } /** * 判断字符串是否是手机号码. * * @param mobile * the mobile * @return 如果 mobile 是null或者empty,返回 false
*/ public static boolean isMobile(String mobile){ return isMatches(MOBILEPHONE, mobile); } //--------------------------------------------------------------- /** * 判断给定的email 字符串是不是邮箱. * * @param email * the email * @return 如果 email 是null或者empty,返回 false
* 如果 email 以. 结尾,返回 false
* 如果 email 不匹配 {@link EmailValidator#EMAIL_PATTERN} ,返回 false
* email 提取 user 部分值,如果不是有效的user,{@link EmailValidator#isValidUser(String)},返回 false
* email 提取 domain 部分值,如果不是有效的domain {@link EmailValidator#isValidDomain(String)},返回 false
* 其余返回true
* * @see com.feilong.lib.validator.EmailValidator * @see how-to-validate-email * -address-with-regular-expression * @see Email_address * */ public static boolean isEmail(String email){ if (isNullOrEmpty(email)){ return false; } EmailValidator emailValidator = com.feilong.lib.validator.EmailValidator.getInstance(); return emailValidator.isValid(email); } //--------------------------------------------------------------- /** * 编译给定正则表达式 regexPattern ,并尝试将给定输入 input 与其匹配. * * @param pattern * 正则表达式 * @param input * 输入的文本 * @return 如果 pattern 是null,抛出 {@link NullPointerException}
* 如果 pattern 是blank,抛出 {@link IllegalArgumentException}
* 如果 input 是null 或者 是blank,返回 false
* @since 1.13.2 if input is blank not redirect return false * @see #289 */ public static boolean isMatches(String pattern,String input){ Validate.notBlank(pattern, "pattern can't be blank!"); return RegexUtil.matches(pattern, input); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy