首页 > 代码库 > Validator
Validator
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
public class Validator
{
/**
* 是否必须有值 <功能详细描述>
*
* @param value
* @return boolean
* @see [类、类#方法、类#成员]
*/
public static boolean isRequired(String value)
{
boolean isFieldValid = false;
if (value != null && value.trim().length() > 0)
{
isFieldValid = true;
}
return isFieldValid;
}
/**
* 验证是否为int型数据 <功能详细描述>
*
* @param value
* @return boolean
* @see [类、类#方法、类#成员]
*/
public static boolean isInt(String value)
{
boolean isFieldValid = false;
try
{
Integer.parseInt(value);
isFieldValid = true;
}
catch (Exception e)
{
isFieldValid = false;
}
return isFieldValid;
}
/**
* 验证value的长度在min和max之间 <功能详细描述>
*
* @param value
* @param minLength 最小长度
* @param maxLength 最大长度
* @return boolean
* @see [类、类#方法、类#成员]
*/
public static boolean isValidateLength(String value, int minLength, int maxLength)
{
String validatedValue = http://www.mamicode.com/value;
if (!isRequired(value))
{
validatedValuehttp://www.mamicode.com/= "";
}
return (validatedValue.length() >= minLength && validatedValue.length() <= maxLength);
}
/**
* 验证value大小在Min和Max之间 <功能详细描述>
*
* @param value
* @param min
* @param max
* @return boolean
* @see [类、类#方法、类#成员]
*/
public static boolean validateRange(int value, int min, int max)
{
return (value >= min && value <= max);
}
/**
* 白名单验证 <功能详细描述>
*
* @param options 白名单
* @param value 待验证对象
* @return boolean
* @see [类、类#方法、类#成员]
*/
public static boolean validateOption(Object[] options, Object value)
{
boolean isValidValue = http://www.mamicode.com/false;
try
{
List list = Arrays.asList(options);
if (list != null)
{
isValidValue = http://www.mamicode.com/list.contains(value);
}
}
catch (Exception e)
{
}
return isValidValue;
}
/**
* Java 1.4以上,正则表达式验证 例如Validator.matchPattern(userName, "^[a-zA-Z0-9]*$") <功能详细描述>
*
* @param value
* @param expression
* @return boolean
* @see [类、类#方法、类#成员]
*/
public static boolean matchPattern(String value, String expression)
{
boolean match = false;
if (isRequired(expression))
{
match = Pattern.matches(expression, value);
}
return match;
}
/**
* cookie 值使用 javax.servlet.http.Cookie 对象来验证 cookie
* <功能详细描述>
*/
/**
* // Example to validate a required cookie value // First retrieve all available cookies submitted in the HTTP
* request Cookie[] cookies = request.getCookies(); if (cookies != null) { // find the "user" cookie for (int i=0; i<cookies.length;
* ++i) { if (cookies[i].getName().equals("user")) { // validate the cookie value if
* (Validator.validateRequired(cookies[i].getValue()) { // valid cookie value, continue processing request } } } }
*/
/** 敏感字符转换成实体字符:< > " ‘ % ; ) ( & +
* <功能详细描述>
* @param value
* @return
* @see [类、类#方法、类#成员]
*/
public static String filterChar(String value)
{
if (value =http://www.mamicode.com/= null)
{
return null;
}
StringBuffer result = new StringBuffer(value.length());
for (int i = 0; i < value.length(); ++i)
{
switch (value.charAt(i))
{
case ‘<‘:
result.append("<");
break;
case ‘>‘:
result.append(">");
break;
case ‘"‘:
result.append(""");
break;
case ‘\‘‘:
result.append("'");
break;
case ‘%‘:
result.append("%");
break;
case ‘;‘:
result.append(";");
break;
case ‘(‘:
result.append("(");
break;
case ‘)‘:
result.append(")");
break;
case ‘&‘:
result.append("&");
break;
case ‘+‘:
result.append("+");
break;
default:
result.append(value.charAt(i));
break;
}
}
return result.toString();
}
}