/** * * @author : bless<[email protected]> * @version : 1.00 * Create Time : 2011-2-22-下午04:31:36 * Description : * 内容校验类 */ public class ValidateUtil implements Serializable{ private static final long serialVersionUID = 1L; /** * 默许编码格局 */ private static final String DEFAULT_ENCODING = "UTF-8"; /** * Function : 检讨字符串的格局 * @author : bless<[email protected]> * @param str : 被检讨的字符串 * @param hasCN : 许可有中文 * @param hasNum : 许可有数字 * @param hasLetter : 许可有字母 * @param specialChars : 许可有特别字符,输入方法:假定许可下划线则参数为,"_" 假设许可下划线和问号则参数为,"_","?" * example : 字母、数字、问号、句号和感慨号组成的字符串:isRealChar("xxxx",false,true,true,"?","。","!") * @return 匹配则返回true,不匹配返回false */ public static boolean isRealChar(String str,boolean hasCN,boolean hasNum,boolean hasLetter,String... specialChars){ String regex_start = "^["; String regex_end = "]+$"; if(hasCN == true){ regex_start = regex_start + "|\u4e00-\u9fa5"; }else if(hasNum == true){ regex_start = regex_start + "|0-9"; }else if(hasLetter == true){ regex_start = regex_start + "|a-zA-Z"; } if(specialChars != null){ for(int i = 0;i<specialChars.length;i++){ regex_start = regex_start +"|"+specialChars[i]; } } return match(regex_start+regex_end,str); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午04:37:22 * Description : 验证邮箱的有效性 * 胜利匹配则返回true * 不匹配则返回false */ public static boolean isEmail(String str) { //return str.matches("[\w\.\-]+@([\w\-]+\.)+[\w\-]+") ; String regex = "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; return match(regex, str); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午04:38:38 * Description :验证IP地址 * 匹配返回true * 不匹配返回false */ public static boolean isIP(String str) { String num = "(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)"; String regex = "^" + num + "\." + num + "\." + num + "\." + num + "$"; return match(regex, str); } /** * * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午04:47:59 * Description : 校验网址(规矩:以https://或https://开头) * 匹配则返回true * 不匹配则返回false */ public static boolean isUrl(String str) { String regex = "http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"; return match(regex, str); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午04:49:46 * Description :验证是不是是固定电话(规矩:xxxx(区号,3或4位)-(衔接线,必备)xxxxxx(电话,6到8位),如028-83035137) * 匹配则返回true * 不匹配则返回false */ public static boolean isTelephone(String str) { String regex = "^(\d{3,4}-)?\d{6,8}$"; return match(regex, str); } /** * @param value * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午04:51:00 * Description : 验证是不是是手机号(规矩:以1开头,接任意数字,11位) * 匹配则返回true * 不匹配则返回false */ public static boolean isMobilePhone(String value) { //String regex = "^[1]+[3,5]+\d{9}$"; String regex = "^[1]+\d{10}"; return value.matches(regex); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:01:02 * Description :邮编地址 * 匹配则返回true * 不匹配则返回false */ public static boolean isPostalcode(String str) { String regex = "^\d{6}$"; return match(regex, str); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:03:46 * Description :验证15位或18位身份证号 * 匹配则返回true * 不匹配则返回false */ public static boolean isIDcard(String str) { String regex = "(^\d{18}$)|(^\d{15}$)"; return match(regex, str); } /** * @param str 被验证数据 * @param digit 验证长度:小数点后几位 * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:12:03 * Description : 验证是不是符合指定位数的小数 * 匹配则返回true * 不匹配则返回false */ public static boolean isDecimal(String str,int digit) { String regex = "^[0-9]+(.[0-9]{"+digit+"})?$"; return match(regex, str); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:18:49 * Description :验证是不是是1~12月 * 匹配则返回true * 不匹配则返回false */ public static boolean isMonth(String str) { String regex = "^(0?[1-9]|1[0-2])$"; return match(regex, str); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:22:57 * Description : 校验日期格局(格局:xxxx-xx-xx,并且斟酌闰月、大小月的情形) * 匹配则返回true * 不匹配则返回false */ public static boolean isDate(String str) { //严厉验证时光格局的(匹配[2002-01-31], [1997-04-30], [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01]) String regex = "^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$"; //没加时光验证的YYYY-MM-DD // String regex = "^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"; //加了时光验证的YYYY-MM-DD 00:00:00 // String regex = "^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$"; return match(regex, str); } /** * @param str * @param sign : 三种"+","-","all",分离表现正数、负数、正负都行 * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:31:02 * Description :校验指定类型的数字 * 匹配则返回true * 不匹配则返回false */ public static boolean isNumber(String str,String sign) { String regex = ""; if("+".equals(sign)){ regex = "^[+]?[0-9]*$"; }else if("-".equals(sign)){ regex = "^[-][0-9]*$"; }else{ regex = "^[+-]?[0-9]*$"; } return match(regex, str); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:36:04 * Description :验证非0正整数 * 匹配则返回true * 不匹配则返回false */ public static boolean isIntNumber(String str) { String regex = "^\+?[1-9][0-9]*$"; return match(regex, str); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:38:38 * Description :验证大写字母 * 匹配则返回true * 不匹配则返回false */ public static boolean isUpChar(String str) { String regex = "^[A-Z]+$"; return match(regex, str); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:39:14 * Description : 验证小写字母 * 匹配则返回true * 不匹配则返回false */ public static boolean isLowChar(String str) { String regex = "^[a-z]+$"; return match(regex, str); } /** * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:39:57 * Description :验证输入的是字母 * 匹配则返回true * 不匹配则返回false */ public static boolean isLetter(String str) { String regex = "^[A-Za-z]+$"; return match(regex, str); } /** * @param str * @param encoding : 编码格局,如GBK, 不输入默以为UTF-8 * @return :返回指定编码格局下字符串的长度 * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午05:54:16 * Description :选择不同编码,返回字符串长度 */ public static int checkLength(String str,String encoding) { int length; try { if(encoding == null||"".equals(encoding)){ length = str.getBytes(DEFAULT_ENCODING).length; }else{ length = str.getBytes(encoding).length; } } catch (UnsupportedEncodingException e) { length = str.getBytes().length; } return length; } /** * @param regex * @param str * @return * author : bless<[email protected]> * about version :1.00 * create time : 2011-2-22-下午04:41:57 * Description : 正则表达式验证办法 * 匹配表达式则返回true * 不匹配则返回false */ private static boolean match(String regex, String str) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); return matcher.matches(); } } /** * * @author : bless<[email protected]> *