/**
*
* @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 : ( : http:// 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();
}
}