Java 正则表达式数据校验

  1. package com.chinadatapay.util;  
  2.   
  3. import java.util.regex.Pattern;  
  4.   
  5. /** 
  6.  * 校验器:利用正则表达式校验邮箱、手机号等 
  7.  * @author Cloud 
  8.  * 
  9.  */  
  10.   
  11. public class Validator {  
  12.       
  13.     /** 
  14.      * 正则表达式:验证用户名 
  15.      */  
  16.     public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";  
  17.    
  18.     /** 
  19.      * 正则表达式:验证密码 
  20.      */  
  21.     public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";  
  22.    
  23.     /** 
  24.      * 正则表达式:验证手机号 
  25.      */  
  26.     public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(14[57])|(17[0])|(17[7])|(18[0,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.      * @param username 
  57.      * @return 校验通过返回true,否则返回false 
  58.      */  
  59.     public static boolean isUsername(String username) {  
  60.         return Pattern.matches(REGEX_USERNAME, username);  
  61.     }  
  62.    
  63.     /** 
  64.      * 校验密码 
  65.      * 
  66.      * @param password 
  67.      * @return 校验通过返回true,否则返回false 
  68.      */  
  69.     public static boolean isPassword(String password) {  
  70.         return Pattern.matches(REGEX_PASSWORD, password);  
  71.     }  
  72.    
  73.     /** 
  74.      * 校验手机号 
  75.      * 
  76.      * @param mobile 
  77.      * @return 校验通过返回true,否则返回false 
  78.      */  
  79.     public static boolean isMobile(String mobile) {  
  80.         return Pattern.matches(REGEX_MOBILE, mobile);  
  81.     }  
  82.    
  83.     /** 
  84.      * 校验邮箱 
  85.      * 
  86.      * @param email 
  87.      * @return 校验通过返回true,否则返回false 
  88.      */  
  89.     public static boolean isEmail(String email) {  
  90.         return Pattern.matches(REGEX_EMAIL, email);  
  91.     }  
  92.    
  93.     /** 
  94.      * 校验汉字 
  95.      * 
  96.      * @param chinese 
  97.      * @return 校验通过返回true,否则返回false 
  98.      */  
  99.     public static boolean isChinese(String chinese) {  
  100.         return Pattern.matches(REGEX_CHINESE, chinese);  
  101.     }  
  102.    
  103.     /** 
  104.      * 校验身份证 
  105.      * 
  106.      * @param idCard 
  107.      * @return 校验通过返回true,否则返回false 
  108.      */  
  109.     public static boolean isIDCard(String idCard) {  
  110.         return Pattern.matches(REGEX_ID_CARD, idCard);  
  111.     }  
  112.    
  113.     /** 
  114.      * 校验URL 
  115.      * 
  116.      * @param url 
  117.      * @return 校验通过返回true,否则返回false 
  118.      */  
  119.     public static boolean isUrl(String url) {  
  120.         return Pattern.matches(REGEX_URL, url);  
  121.     }  
  122.    
  123.     /** 
  124.      * 校验IP地址 
  125.      * 
  126.      * @param ipAddr 
  127.      * @return 
  128.      */  
  129.     public static boolean isIPAddr(String ipAddr) {  
  130.         return Pattern.matches(REGEX_IP_ADDR, ipAddr);  
  131.     }  
  132. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值