ValidationUtils.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package com.fdkankan.common.validation;
  2. import org.springframework.util.StringUtils;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. public class ValidationUtils {
  8. /** Email */
  9. 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}$" ;
  10. /** Age 1-120 */
  11. public static final String AGE_PATTERN="^(?:[1-9][0-9]?|1[01][0-9]|120)$";
  12. /** URL、http、www、ftp */
  13. public static final String URL_PATTERN = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?" ;
  14. /** No. */
  15. public static final String INTEGER_PATTERN = "^([+-]?)\\d*\\.?\\d+$" ;
  16. /** String. */
  17. public static final String STRING_PATTERN = "^[\\u4E00-\\u9FFF\\sA-za-z]+$";
  18. // public static final String STRING_PATTERN = "^[\\u4E00-\\u9FFF\\sA-za-z\\d*]+$";
  19. /** Chinese. */
  20. public static final String CHINESE_NAME = "^[\\u4E00-\\u9FFF\\W]+$";
  21. /** PhoenNo. */
  22. public static final String PHONE_NO = "^[0-9 ]{1,20}$";
  23. /** PhoneCountry. */
  24. public static final String PHONE_COUNTRY = "^[0-9\\ \\+\\-\\(\\)]{1,22}$";
  25. /** HkID. */
  26. public static final String HK_ID = "[a-zA-Z]{1}[0-9a-zA-Z]{6}\\([0-9]{1}\\)";
  27. /** Only String and space */
  28. public static final String ONLY_STRING_SPACE="^[A-Za-z\\s\\u4E00-\\u9FFF\\W]+$";
  29. /** RecStatus */
  30. public static boolean validateRecStatus(String str){
  31. if(StringUtils.isEmpty(str)){
  32. return Boolean.FALSE ;
  33. }
  34. if ("A".equals(str) || "I".equals(str)){
  35. return Boolean.TRUE;
  36. }
  37. return Boolean.FALSE;
  38. }
  39. /**
  40. * plan、marital
  41. */
  42. public static boolean validateInteger(int i){
  43. if(i == 1 || i == 2 || i == 3 || i == 4 || i == 5){
  44. return Boolean.TRUE ;
  45. }
  46. return Boolean.FALSE;
  47. }
  48. public static boolean validateString(String i){
  49. if("1".equals(i) || "2".equals(i)){
  50. return Boolean.TRUE ;
  51. }
  52. return Boolean.FALSE;
  53. }
  54. /** Email */
  55. public static boolean validateEamil(final String email){
  56. if(StringUtils.isEmpty(email)){
  57. return Boolean.FALSE ;
  58. }
  59. return email.matches(EMAIL_PATTERN) ;
  60. }
  61. /** URL */
  62. public static boolean validateUrl(final String url){
  63. if(StringUtils.isEmpty(url)){
  64. return Boolean.FALSE ;
  65. }
  66. return url.matches(URL_PATTERN) ;
  67. }
  68. /**
  69. * Date
  70. * str to Date check
  71. */
  72. public static boolean validateDate(String date){
  73. if(StringUtils.isEmpty(date)){
  74. return Boolean.FALSE ;
  75. }
  76. try {
  77. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  78. dateFormat.parse(date);
  79. } catch (ParseException e) {
  80. return Boolean.FALSE;
  81. }
  82. return Boolean.TRUE;
  83. }
  84. /**
  85. * Date
  86. * date check
  87. */
  88. public static boolean validateDate(Date date){
  89. try {
  90. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  91. formatter.format(date);
  92. if(date.after(new Date())){
  93. return Boolean.FALSE;
  94. }
  95. } catch (Exception e) {
  96. return Boolean.FALSE;
  97. }
  98. return Boolean.TRUE;
  99. }
  100. /** Message */
  101. public static boolean validateMsg(String name, int start , int end){
  102. if(StringUtils.isEmpty(name)){
  103. return Boolean.FALSE ;
  104. }
  105. if (name.length() >= start && name.length() <= end){
  106. return Boolean.TRUE ;
  107. }
  108. return Boolean.FALSE;
  109. }
  110. /** PhoenNo */
  111. public static boolean validatePhoenNo(String str){
  112. if(StringUtils.isEmpty(str)){
  113. return Boolean.FALSE ;
  114. }
  115. return str.matches(PHONE_NO) ;
  116. }
  117. /** PhoneCountry */
  118. public static boolean validatePhoneCountry(String str){
  119. if(StringUtils.isEmpty(str)){
  120. return Boolean.FALSE ;
  121. }
  122. return str.matches(PHONE_COUNTRY) ;
  123. }
  124. /** check Float */
  125. public static boolean validateFloat(String str){
  126. if(StringUtils.isEmpty(str)){
  127. return Boolean.FALSE ;
  128. }
  129. String key = "[+]?[0-9]*\\.?[0-9]{2}+";
  130. return str.matches(key) ;
  131. }
  132. /** compareDate */
  133. public static boolean compareDate(String str){
  134. if(StringUtils.isEmpty(str)){
  135. return Boolean.FALSE ;
  136. }
  137. try {
  138. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  139. Date date = dateFormat.parse(str);
  140. Calendar calendar = Calendar.getInstance();
  141. calendar.setTime(new Date());
  142. calendar.add(Calendar.DAY_OF_MONTH, -15);
  143. Date nowDate = dateFormat.parse(dateFormat.format(calendar.getTime()));
  144. if (date.before(nowDate)){
  145. return Boolean.TRUE;
  146. } else {
  147. return Boolean.FALSE;
  148. }
  149. } catch (ParseException e) {
  150. return Boolean.FALSE;
  151. }
  152. }
  153. public static boolean validateState(int i){
  154. if(i == 0 || i == 1 ){
  155. return Boolean.TRUE ;
  156. }
  157. return Boolean.FALSE;
  158. }
  159. }