package com.fdkankan.common.validation; import org.springframework.util.StringUtils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class ValidationUtils { /** Email */ public static final String EMAIL_PATTERN = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z0-9]{2,40}$" ; /** Age 1-120 */ public static final String AGE_PATTERN="^(?:[1-9][0-9]?|1[01][0-9]|120)$"; /** URL、http、www、ftp */ public static final String URL_PATTERN = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?" ; /** No. */ public static final String INTEGER_PATTERN = "^([+-]?)\\d*\\.?\\d+$" ; /** String. */ public static final String STRING_PATTERN = "^[\\u4E00-\\u9FFF\\sA-za-z]+$"; // public static final String STRING_PATTERN = "^[\\u4E00-\\u9FFF\\sA-za-z\\d*]+$"; /** Chinese. */ public static final String CHINESE_NAME = "^[\\u4E00-\\u9FFF\\W]+$"; /** PhoenNo. */ public static final String PHONE_NO = "^[0-9 ]{1,20}$"; /** PhoneCountry. */ public static final String PHONE_COUNTRY = "^[0-9\\ \\+\\-\\(\\)]{1,22}$"; /** HkID. */ public static final String HK_ID = "[a-zA-Z]{1}[0-9a-zA-Z]{6}\\([0-9]{1}\\)"; /** Only String and space */ public static final String ONLY_STRING_SPACE="^[A-Za-z\\s\\u4E00-\\u9FFF\\W]+$"; /** RecStatus */ public static boolean validateRecStatus(String str){ if(StringUtils.isEmpty(str)){ return Boolean.FALSE ; } if ("A".equals(str) || "I".equals(str)){ return Boolean.TRUE; } return Boolean.FALSE; } /** * plan、marital */ public static boolean validateInteger(int i){ if(i == 1 || i == 2 || i == 3 || i == 4 || i == 5){ return Boolean.TRUE ; } return Boolean.FALSE; } public static boolean validateString(String i){ if("1".equals(i) || "2".equals(i)){ return Boolean.TRUE ; } return Boolean.FALSE; } /** Email */ public static boolean validateEamil(final String email){ if(StringUtils.isEmpty(email)){ return Boolean.FALSE ; } return email.matches(EMAIL_PATTERN) ; } /** URL */ public static boolean validateUrl(final String url){ if(StringUtils.isEmpty(url)){ return Boolean.FALSE ; } return url.matches(URL_PATTERN) ; } /** * Date * str to Date check */ public static boolean validateDate(String date){ if(StringUtils.isEmpty(date)){ return Boolean.FALSE ; } try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.parse(date); } catch (ParseException e) { return Boolean.FALSE; } return Boolean.TRUE; } /** * Date * date check */ public static boolean validateDate(Date date){ try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); formatter.format(date); if(date.after(new Date())){ return Boolean.FALSE; } } catch (Exception e) { return Boolean.FALSE; } return Boolean.TRUE; } /** Message */ public static boolean validateMsg(String name, int start , int end){ if(StringUtils.isEmpty(name)){ return Boolean.FALSE ; } if (name.length() >= start && name.length() <= end){ return Boolean.TRUE ; } return Boolean.FALSE; } /** PhoenNo */ public static boolean validatePhoenNo(String str){ if(StringUtils.isEmpty(str)){ return Boolean.FALSE ; } return str.matches(PHONE_NO) ; } /** PhoneCountry */ public static boolean validatePhoneCountry(String str){ if(StringUtils.isEmpty(str)){ return Boolean.FALSE ; } return str.matches(PHONE_COUNTRY) ; } /** check Float */ public static boolean validateFloat(String str){ if(StringUtils.isEmpty(str)){ return Boolean.FALSE ; } String key = "[+]?[0-9]*\\.?[0-9]{2}+"; return str.matches(key) ; } /** compareDate */ public static boolean compareDate(String str){ if(StringUtils.isEmpty(str)){ return Boolean.FALSE ; } try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = dateFormat.parse(str); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_MONTH, -15); Date nowDate = dateFormat.parse(dateFormat.format(calendar.getTime())); if (date.before(nowDate)){ return Boolean.TRUE; } else { return Boolean.FALSE; } } catch (ParseException e) { return Boolean.FALSE; } } public static boolean validateState(int i){ if(i == 0 || i == 1 ){ return Boolean.TRUE ; } return Boolean.FALSE; } }