package com.tongrong.utils;import java.util.Collection;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.commons.lang.StringUtils;/** * Java表單驗證工具類 * * @author jiqinlin * */@SuppressWarnings("unchecked")public class RegexUtil { public static void main(String[] args) {// System.out.println("過濾中英文特殊字元: "+RegexUtil.stringFilter("中國~~!#$%%."));// System.out.println("是否包含中英文特殊字元: "+RegexUtil.isContainsSpecialChar("12"));// System.out.println("過濾html代碼: "+RegexUtil.htmltoText("<JAVASCRIPT>12</JAVASCRIPT>DDDDD"));// System.out.println("判斷中文字元: "+RegexUtil.isChineseChar("中國。")); System.out.println("匹配漢字: "+RegexUtil.isChinese("中國。"));// System.out.println("判斷英文字元: "+RegexUtil.isEnglish("abc!"));// System.out.println("判斷合法字元: "+RegexUtil.isRightfulString("abc_-11AAA"));// System.out.println("郵遞區號驗證: "+RegexUtil.isZipCode("162406"));// System.out.println("社會安全號碼碼驗證: "+RegexUtil.isIdCardNo("35052419880210133e"));// System.out.println("手機號碼驗證: "+RegexUtil.isMobile("18918611111"));// System.out.println("電話號碼驗證: "+RegexUtil.isPhone("8889333"));// System.out.println("電話號碼驗證: "+RegexUtil.isNumeric("888.9333"));// System.out.println("匹配密碼: "+RegexUtil.isPwd("d888d_ddddd"));// System.out.println("匹配密碼: "+RegexUtil.isUrl("http://baidu.com")); System.out.println("驗證字元: "+RegexUtil.stringCheck("中文aabc001_-"));// System.out.println(isEmail("416501600@qq.com")); //http://baidu.com www.baidu.com baidu.com// System.out.println(NumberUtils.toInt("-0000000002")); } public final static boolean isNull(Object[] objs){ if(objs==null||objs.length==0) return true; return false; } public final static boolean isNull(Integer integer){ if(integer==null||integer==0) return true; return false; } public final static boolean isNull(Collection collection){ if(collection==null||collection.size()==0) return true; return false; } public final static boolean isNull(Map map){ if(map==null||map.size()==0) return true; return false; } public final static boolean isNull(String str){ return str == null || "".equals(str.trim()) || "null".equals(str.toLowerCase()); } public final static boolean isNull(Long longs){ if(longs==null||longs==0) return true; return false; } public final static boolean isNotNull(Long longs){ return !isNull(longs); } public final static boolean isNotNull(String str){ return !isNull(str); } public final static boolean isNotNull(Collection collection){ return !isNull(collection); } public final static boolean isNotNull(Map map){ return !isNull(map); } public final static boolean isNotNull(Integer integer){ return !isNull(integer); } public final static boolean isNotNull(Object[] objs){ return !isNull(objs); } /** * 匹配URL地址 * * @param str * @return * @author jiqinlin */ public final static boolean isUrl(String str) { return match(str, "^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"); } /** * 匹配密碼,以字母開頭,長度在6-12之間,只能包含字元、數字和底線。 * * @param str * @return * @author jiqinlin */ public final static boolean isPwd(String str) { return match(str, "^[a-zA-Z]\\w{6,12}$"); } /** * 驗證字元,只能包含中文、英文、數字、底線等字元。 * * @param str * @return * @author jiqinlin */ public final static boolean stringCheck(String str) { return match(str, "^[a-zA-Z0-9\u4e00-\u9fa5-_]+$"); } /** * 匹配Email地址 * * @param str * @return * @author jiqinlin */ public final static boolean isEmail(String str) { return match(str, "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"); } /** * 匹配非負整數(正整數+0) * * @param str * @return * @author jiqinlin */ public final static boolean isInteger(String str) { return match(str, "^[+]?\\d+$"); } /** * 判斷數實值型別,包括整數和浮點數 * * @param str * @return * @author jiqinlin */ public final static boolean isNumeric(String str) { if(isFloat(str) || isInteger(str)) return true; return false; } /** * 只能輸入數字 * * @param str * @return * @author jiqinlin */ public final static boolean isDigits(String str) { return match(str, "^[0-9]*$"); } /** * 匹配正浮點數 * * @param str * @return * @author jiqinlin */ public final static boolean isFloat(String str) { return match(str, "^[-\\+]?\\d+(\\.\\d+)?$"); } /** * 聯絡電話(手機/電話皆可)驗證 * * @param text * @return * @author jiqinlin */ public final static boolean isTel(String text){ if(isMobile(text)||isPhone(text)) return true; return false; } /** * 電話號碼驗證 * * @param text * @return * @author jiqinlin */ public final static boolean isPhone(String text){ return match(text, "^(\\d{3,4}-?)?\\d{7,9}$"); } /** * 手機號碼驗證 * * @param text * @return * @author jiqinlin */ public final static boolean isMobile(String text){ if(text.length()!=11) return false; return match(text, "^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\\d{8})$"); } /** * 社會安全號碼碼驗證 * * @param text * @return * @author jiqinlin */ public final static boolean isIdCardNo(String text){ return match(text, "^(\\d{6})()?(\\d{4})(\\d{2})(\\d{2})(\\d{3})(\\w)$"); } /** * 郵遞區號驗證 * * @param text * @return * @author jiqinlin */ public final