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

使用的时候,需要注意手机号第二位是否出现新的。如:7就是后面加的。因为有了170开头的手机号

   
public class RegexUtil {
   
   /**
     * 正则表达式:验证手机号
     */
    public static final String REGEX_MOBILE = "^[\\d]{11}[\\d]*$";
    /**
     * 正则表达式:验证汉字
     */
    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";
    /**
     * 正则表达式:验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";
   /**
    * 判断字符串是否是由纯数字组成
    * @param str
    * @return
    */
   public static boolean isNumeric(String str) {
      Pattern pattern = Pattern.compile("[0-9]*");
      Matcher isNum = pattern.matcher(str);
      if (!isNum.matches()) {
         return false;
      }
      return true;
   }
    /**
     * 校验手机号
     * 
     * @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 allIsMobile(String mobile) {
        if (StringUtil.isBlank(mobile)){
            throw new UserException(UserErrorCodeEnum.ILLGAL_ARGUMENT);
        }
        String[] child = mobile.split(",");
        boolean result = Boolean.TRUE;
        for (int i = 0; i < child.length; i++) {
            if (!Pattern.matches(REGEX_MOBILE, child[i])){
                result = Boolean.FALSE;
            }
        }
        return result;
    }

    /**
     * 校验用户名
     * 
     * @param username
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isUsername(String username) {
        return Pattern.matches(REGEX_USERNAME, username);
    }
    
    /**
     * 校验汉字
     * @return 校验通过返回true,否则返回false
     */
 // 判断一个字符串是否含有中文
    public static boolean isChinese(String str) {
        if (str == null) return false;
        for (char c : str.toCharArray()) {
            if (isChinese(c)) return true;// 有一个中文字符就返回
        }
        return false;
    }
   // 判断一个字符是否是中文
    public static boolean isChinese(char c) {
          return c >= 0x4E00 &&  c <= 0x9FA5;// 根据字节码判断
    }
    
   public static void main(String[] args) {
        String str = "15757174784,15624587485,15687456546";
        System.out.println(allIsMobile(str));
   }
}




/** * 匹配手机号的规则。中间的3578,是手机号第二位可能出现的情况 */ public static final String REGEX_MOBILE = "^[1][3578][0-9]{9}$"; /** * 校验手机号 * @param mobile * @return 校验通过返回true,否则返回false */ public static boolean isMobile(String mobile) { return Pattern.matches(REGEX_MOBILE, mobile);

}

[html] view plain copy
  1. package com.weshop.core.util;  
  2. import java.util.regex.Pattern;  
  3.   
  4. /**  
  5.  * 校验器:利用正则表达式校验邮箱、手机号等  
  6.  *   
  7.  * @author   
  8.  *   
  9.  */  
  10. public class Validator {  
  11.     /**  
  12.      * 正则表达式:验证用户名  
  13.      */  
  14.     //public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";  
  15.     public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{2,20}$";  
  16.         
  17.     /**  
  18.      * 正则表达式:验证密码  
  19.      */  
  20.     public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";  
  21.     
  22.     /**  
  23.      * 正则表达式:验证手机号  
  24.      */  
  25.    // public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";  
  26.     public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0-9]))\\d{8}$";  
  27.       
  28.     /**  
  29.      * 正则表达式:验证邮箱  
  30.      */  
  31.     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,}$";  
  32.     
  33.     /**  
  34.      * 正则表达式:验证汉字  
  35.      */  
  36.     public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";  
  37.     
  38.     /**  
  39.      * 正则表达式:验证身份证  
  40.      */  
  41.     public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";  
  42.     
  43.     /**  
  44.      * 正则表达式:验证URL  
  45.      */  
  46.     public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";  
  47.     
  48.     /**  
  49.      * 正则表达式:验证IP地址  
  50.      */  
  51.     public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";  
  52.     
  53.     /**  
  54.      * 正则表达式:验证图片参数  
  55.      */  
  56.     public static final String REGEX_IMG_URL = "^.*_[1-9]\\d*x[1-9]\\d*.jpg$";  
  57.     /**  
  58.      * 校验用户名  
  59.      *   
  60.      * @param username  
  61.      * @return 校验通过返回true,否则返回false  
  62.      */  
  63.     public static boolean isUsername(String username) {  
  64.         return Pattern.matches(REGEX_USERNAME, username);  
  65.     }  
  66.     
  67.       
  68.     public static boolean isImgParameter(String p)  
  69.     {  
  70.         return Pattern.matches(REGEX_IMG_URL, p);  
  71.           
  72.     }  
  73.     /**  
  74.      * 校验密码  
  75.      *   
  76.      * @param password  
  77.      * @return 校验通过返回true,否则返回false  
  78.      */  
  79.     public static boolean isPassword(String password) {  
  80.         return Pattern.matches(REGEX_PASSWORD, password);  
  81.     }  
  82.     
  83.     /**  
  84.      * 校验手机号  
  85.      *   
  86.      * @param mobile  
  87.      * @return 校验通过返回true,否则返回false  
  88.      */  
  89.     public static boolean isMobile(String mobile) {  
  90.         return Pattern.matches(REGEX_MOBILE, mobile);  
  91.     }  
  92.     
  93.     /**  
  94.      * 校验邮箱  
  95.      *   
  96.      * @param email  
  97.      * @return 校验通过返回true,否则返回false  
  98.      */  
  99.     public static boolean isEmail(String email) {  
  100.         return Pattern.matches(REGEX_EMAIL, email);  
  101.     }  
  102.     
  103.     /**  
  104.      * 校验汉字  
  105.      *   
  106.      * @param chinese  
  107.      * @return 校验通过返回true,否则返回false  
  108.      */  
  109.     public static boolean isChinese(String chinese) {  
  110.         return Pattern.matches(REGEX_CHINESE, chinese);  
  111.     }  
  112.     
  113.     /**  
  114.      * 校验身份证  
  115.      *   
  116.      * @param idCard  
  117.      * @return 校验通过返回true,否则返回false  
  118.      */  
  119.     public static boolean isIDCard(String idCard) {  
  120.         return Pattern.matches(REGEX_ID_CARD, idCard);  
  121.     }  
  122.     
  123.     /**  
  124.      * 校验URL  
  125.      *   
  126.      * @param url  
  127.      * @return 校验通过返回true,否则返回false  
  128.      */  
  129.     public static boolean isUrl(String url) {  
  130.         return Pattern.matches(REGEX_URL, url);  
  131.     }  
  132.     
  133.     /**  
  134.      * 校验IP地址  
  135.      *   
  136.      * @param ipAddr  
  137.      * @return  
  138.      */  
  139.     public static boolean isIPAddr(String ipAddr) {  
  140.         return Pattern.matches(REGEX_IP_ADDR, ipAddr);  
  141.     }  
  142.     
  143.     public static void main(String[] args) {  
  144.        // String username = "fdsdfsdj";  
  145.        // System.out.println(Validator.isUsername(username));  
  146.        // System.out.println(Validator.isChinese(username));  
  147.         System.out.println(Validator.isImgParameter("http://127.0.0.1:8080/ddbuy/c.jpg_1400x20.jpg"));  
  148.     }  
  149. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值