常用正则,校验器:利用正则表达式校验邮箱、手机号等

校验器:利用正则表达式校验邮箱、手机号等

package com.baidu.gongyi.auction.util;

import java.util.regex.Pattern;

/**
 * 校验器:利用正则表达式校验邮箱、手机号等
 */
public class ValidateUtils {
    /**
     * 正则表达式:验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";

    /**
     * 正则表达式:验证密码
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";
    /**
     * 正则表达式:验证正整数或0
     */
    public static final String REGEX_POSITIVE_NUMBER = "^([0-9]*[1-9][0-9]*|0)$";

    /**
     * 正则表达式:验证手机号
     */
    public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
    /**
     * 正则表达式:验证手机号,固话,国际固话
     */
    public static final String REGEX_PHONENUM =
            "^((\\+\\d{1,3}(-| )?\\(?\\d\\)?(-| )?\\d{1,5})|(\\(?\\d{2,6}\\)?))(-| )?(\\d{3,4})(-| )?(\\d{4})(( x| ext)\\d{1,5}){0,1}$";

    /**
     * 正则表达式:验证邮箱
     */
    // public static final String REGEX_EMAIL =
    // "^([a-zA-Z0-9_|\\.-])+@[a-zA-Z0-9_-]+(|\\.[a-zA-Z0-9_-]+)$";

    public static final String REGEX_EMAIL =
            "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    /**
     * 正则表达式:验证汉字
     */
    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";

    /**
     * 正则表达式:验证身份证
     */
    public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";

    /**
     * 正则表达式:验证URL
     */
    public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";

    /**
     * 正则表达式:验证IP地址
     */
    public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";

    /**
     * 校验用户名
     *
     * @param username
     *
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isUsername(String username) {
        return Pattern.matches(REGEX_USERNAME, username);
    }

    /**
     * 校验密码
     *
     * @param password
     *
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isPassword(String password) {
        return Pattern.matches(REGEX_PASSWORD, password);
    }

    /**
     * 校验正整数或0
     * 
     * @param number
     * @return
     */
    public static boolean isPositiveNumber(String number) {
        return Pattern.matches(REGEX_POSITIVE_NUMBER, number);
    }

    /**
     * 校验手机号
     *
     * @param mobile
     *
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isMobile(String mobile) {
        return Pattern.matches(REGEX_MOBILE, mobile);
    }

    /**
     * 校验固话,国际固话,手机号
     *
     * @param mobile
     *
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isPhoneNum(String phoneNum) {
        return Pattern.matches(REGEX_PHONENUM, phoneNum);
    }

    /**
     * 校验邮箱
     *
     * @param email
     *
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isEmail(String email) {
        return Pattern.matches(REGEX_EMAIL, email);
    }

    /**
     * 校验汉字
     *
     * @param chinese
     *
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isChinese(String chinese) {
        return Pattern.matches(REGEX_CHINESE, chinese);
    }

    /**
     * 校验身份证
     *
     * @param idCard
     *
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isIDCard(String idCard) {
        return Pattern.matches(REGEX_ID_CARD, idCard);
    }

    /**
     * 校验URL
     *
     * @param url
     *
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isUrl(String url) {
        return Pattern.matches(REGEX_URL, url);
    }

    /**
     * 校验IP地址
     *
     * @param ipAddr
     *
     * @return
     */
    public static boolean isIPAddr(String ipAddr) {
        return Pattern.matches(REGEX_IP_ADDR, ipAddr);
    }

    /**
     * 登录名隐藏
     *
     * @param loginName
     *
     * @return
     */
    public static String escapeLoginName(String loginName) {
        if (loginName == null) {
            return null;
        }
        if (isMobile(loginName)) {
            return loginName.substring(0, 3) + "***" + loginName.substring(loginName.length() - 4, loginName.length());
        } else if (isEmail(loginName)) {
            int pos = loginName.indexOf('@');
            if (pos < 4) {
                return loginName.substring(0, pos - 1) + "*" + loginName.substring(pos, loginName.length());
            } else {
                return loginName.substring(0, 3) + "***" + loginName.substring(pos, loginName.length());
            }
        }
        return "*" + loginName.substring(1, loginName.length());
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值