Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
top.hmtools.base.ValidationTools Maven / Gradle / Ivy
package top.hmtools.base;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 数据合法性验证工具
* @author HyboJ
* 创建日期:2017-1-14下午4:55:03
*/
public class ValidationTools {
/**
* 是否为 null
* @param objs
* @return
*/
public final static boolean isNull(Object[] objs){
if(objs==null||objs.length==0) return true;
return false;
}
/**
* 是否为 null
* @param integer
* @return
*/
public final static boolean isNull(Integer integer){
if(integer==null||integer==0) return true;
return false;
}
/**
* 是否为 null
* @param collection
* @return
*/
public final static boolean isNull(Collection collection){
if(collection==null||collection.size()==0) return true;
return false;
}
/**
* 是否为 null
* @param map
* @return
*/
public final static boolean isNull(Map map){
if(map==null||map.size()==0) return true;
return false;
}
/**
* 是否为 null
* @param str
* @return
*/
public final static boolean isNull(String str){
return str == null || "".equals(str.trim()) || "null".equals(str.toLowerCase());
}
/**
* 是否为 null
* @param longs
* @return
*/
public final static boolean isNull(Long longs){
if(longs==null||longs==0) return true;
return false;
}
/**
* 是否 不为 null
* @param longs
* @return
*/
public final static boolean isNotNull(Long longs){
return !isNull(longs);
}
/**
* 是否 不为 null
* @param str
* @return
*/
public final static boolean isNotNull(String str){
return !isNull(str);
}
/**
* 是否 不为 null
* @param collection
* @return
*/
public final static boolean isNotNull(Collection collection){
return !isNull(collection);
}
/**
* 是否 不为 null
* @param map
* @return
*/
public final static boolean isNotNull(Map map){
return !isNull(map);
}
/**
* 是否 不为 null
* @param integer
* @return
*/
public final static boolean isNotNull(Integer integer){
return !isNull(integer);
}
/**
* 是否 不为 null
* @param objs
* @return
*/
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-16之间,只能包含字符、数字和下划线。
*
* @param str
* @return
* @author jiqinlin
*/
public final static boolean isPwd(String str) {
return match(str, "^[a-zA-Z]\\w{6,16}$");
}
/**
* 匹配密码,以字母开头,长度在min-max之间,只能包含字符、数字和下划线。
* @param str
* @param min 缺省 为 6
* @param max 缺省 为 16
* @return
*/
public final static boolean isPwd(String str,int min,int max) {
if( min <= 0){
min=6;
}
if(max <= min){
max = 16;
}
return match(str, "^[a-zA-Z]\\w{"+min+","+max+"}$");
}
/**
* 验证字符,只能包含中文、英文、数字、下划线等字符。
*
* @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})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\\d{8})$");
}
// 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 static boolean isZipCode(String text){
return match(text, "^[0-9]{6}$");
}
/**
* 判断整数num是否等于0
*
* @param num
* @return
* @author jiqinlin
*/
public final static boolean isIntEqZero(int num){
return num==0;
}
/**
* 判断整数num是否大于0
*
* @param num
* @return
* @author jiqinlin
*/
public final static boolean isIntGtZero(int num){
return num>0;
}
/**
* 判断整数num是否大于或等于0
*
* @param num
* @return
* @author jiqinlin
*/
public final static boolean isIntGteZero(int num){
return num>=0;
}
/**
* 判断浮点数num是否等于0
*
* @param num 浮点数
* @return
* @author jiqinlin
*/
public final static boolean isFloatEqZero(float num){
return num==0f;
}
/**
* 判断浮点数num是否大于0
*
* @param num 浮点数
* @return
* @author jiqinlin
*/
public final static boolean isFloatGtZero(float num){
return num>0f;
}
/**
* 判断浮点数num是否大于或等于0
*
* @param num 浮点数
* @return
* @author jiqinlin
*/
public final static boolean isFloatGteZero(float num){
return num>=0f;
}
/**
* 判断是否为合法字符(a-zA-Z0-9-_)
*
* @param text
* @return
* @author jiqinlin
*/
public final static boolean isRightfulString(String text){
return match(text, "^[A-Za-z0-9_-]+$");
}
/**
* 判断英文字符(a-zA-Z)
*
* @param text
* @return
* @author jiqinlin
*/
public final static boolean isEnglish(String text){
return match(text, "^[A-Za-z]+$");
}
/**
* 判断中文字符(包括汉字和符号)
*
* @param text
* @return
* @author jiqinlin
*/
public final static boolean isChineseChar(String text){
return match(text, "^[\u0391-\uFFE5]+$");
}
/**
* 匹配汉字
*
* @param text
* @return
* @author jiqinlin
*/
public final static boolean isChinese(String text){
return match(text, "^[\u4e00-\u9fa5]+$");
}
/**
* 是否包含中英文特殊字符,除英文"-_"字符外
*
* @param str
* @return
*/
public static boolean isContainsSpecialChar(String text) {
if(StringTools.isBlank(text)) return false;
String[] chars={"[","`","~","!","@","#","$","%","^","&","*","(",")","+","=","|","{","}","'",
":",";","'",",","[","]",".","<",">","/","?","~","!","@","#","¥","%","…","&","*","(",")",
"—","+","|","{","}","【","】","‘",";",":","”","“","’","。",",","、","?","]"};
for(String ch : chars){
if(text.contains(ch)) return true;
}
return false;
}
/**
* 过滤中英文特殊字符,除英文"-_"字符外
*
* @param text
* @return
*/
public static String stringFilter(String text) {
String regExpr="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Pattern p = Pattern.compile(regExpr);
Matcher m = p.matcher(text);
return m.replaceAll("").trim();
}
/**
* 过滤html代码
*
* @param inputString 含html标签的字符串
* @return
*/
public static String htmlFilter(String inputString) {
String htmlStr = inputString; // 含html标签的字符串
String textStr = "";
java.util.regex.Pattern p_script;
java.util.regex.Matcher m_script;
java.util.regex.Pattern p_style;
java.util.regex.Matcher m_style;
java.util.regex.Pattern p_html;
java.util.regex.Matcher m_html;
java.util.regex.Pattern p_ba;
java.util.regex.Matcher m_ba;
try {
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或