Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.44 KB | None | 0 0
  1. package es.rf.tienda.util;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10.  
  11. /********************************************************************************************
  12. * NOMBRE: Validator.java
  13. *
  14. * DESCRIPCION:
  15. * Clase auxiliar para validar los datos que sean
  16. * introducidos en la aplicaci�n.
  17. *
  18. * @version 30/01/2016
  19. * @author Miguel Garcia
  20. *
  21. * ******************************************************************************************/
  22. public class Validator {
  23.  
  24. private static final String ALFANUMERIC_PATTERN = "^[0-9a-zA-Z]+$";
  25.  
  26. private static final String PASSWORD_PATTERN =
  27. "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";
  28. /**
  29. * Patr�n para validar el email, evitando puntos finales antes de la @ y que solo contenga
  30. * car�cteres alfanum�ricos
  31. */
  32. private final static String EMAIL_PATTERN =
  33. "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
  34.  
  35. /**
  36. * Permite verificar que un DNI cumple con el patr�n XX.XXX.XXX-L
  37. */
  38. private final static String DNI_PATTERN = "\\d{2}\\.\\d{3}\\.\\d{3}-[a-zA-Z]";
  39.  
  40. /**
  41. * Permite validar un tel�fono, el cual debe contener de 10 a 20 d�gitos
  42. * y donde los espacios est�n permitidos
  43. */
  44. private final static String PHONE_PATTERN = "[\\d ]{10,20}";
  45.  
  46. /**
  47. * Orden de las letras con las cuales se comprobar� la validez del DNI
  48. */
  49. private final static String LETRA_DNI = "TRWAGMYFPDXBNJZSQVHLCKE";
  50.  
  51. /**
  52. * Longitud que debe tener todo DNI pasado a la aplicaci�n.
  53. */
  54. private final static int LONGITUD_DNI = 12;
  55.  
  56. private final int LONG_MAX_EMAIL_AND_NAME = 100;
  57. private final static int LONG_MAX_PASS = 20;
  58. private final int LONG_MAX_DNI = 12;
  59. private final static int LONG_MIN_PASS = 8;
  60.  
  61. private final int LONG_MIN_NAME = 5;
  62. private final int LONG_MIN_EMAIL = 6;
  63.  
  64. /* ***************************************************************************************
  65. * NOMBRE: isAlfanumeric *
  66. *
  67. * DESCRIPCI�N: *//**
  68. * Permite verificar que el texto pasado solo contiene caracters alfanum�ricos
  69. *
  70. * @param texto String a verificar que solo tenga car�cteres alfanum�ricos
  71. *
  72. * @return true, si cumple solo contiene caracters alfanum�ricos. <br>
  73. * false en caso contrario
  74. * FECHA: Enero 2016
  75. *
  76. * AUTOR: Miguel Garcia - Barcelona
  77. *
  78. * **************************************************************************************/
  79. public static boolean isAlfanumeric(String texto){
  80. return texto.matches(ALFANUMERIC_PATTERN);
  81. }
  82.  
  83. public static boolean isVacio( String prueba ){
  84. if(prueba !=null)
  85. return prueba.length() > 0 && !prueba.equals("");
  86. else
  87. return false;
  88. }
  89.  
  90. /* ***************************************************************************************
  91. * NOMBRE: cumplePhoneNumber *
  92. *
  93. * DESCRIPCI�N: *//**
  94. * El phone number debe tener un total de entre 10 y 20, contando d�gitos y espacios.
  95. * M�nimo aceptable son 10 d�gitos.
  96. *
  97. * @param phoneNumber String con el n�mero de telefono de entre 10 y 20 d�gitos.
  98. * Puede tener espacios, pero siempre con 10 d�gitos como m�nimo.
  99. *
  100. * @return true, si cumple todas las condiciones
  101. *
  102. * FECHA: Enero 2016
  103. * AUTOR: Miguel Garcia
  104. *
  105. * **************************************************************************************/
  106. public static boolean cumplePhoneNumber(String phoneNumber){
  107. return phoneNumber.matches(PHONE_PATTERN);
  108. }
  109.  
  110. /* ***************************************************************************************
  111. * NOMBRE: isEmailValido *
  112. *
  113. * DESCRIPCI�N: *//**
  114. * Comprueba si el email tiene un formato que pueda ser v�lido.
  115. *
  116. * @param email String a comprobar
  117. *
  118. * @return true, en caso que el formato sea v�lido
  119. * FECHA: Enero 2016
  120. *
  121. * AUTOR: Miguel Garcia
  122. *
  123. * **************************************************************************************/
  124. public static boolean isEmailValido(String email){
  125. return email.matches(EMAIL_PATTERN);
  126.  
  127. }
  128.  
  129. /* ***************************************************************************************
  130. * NOMBRE: cumpleDNI *
  131. *
  132. * DESCRIPCI�N: *//**
  133. * Esta funci�n verifica que el DNI cumple el siguiente formato: xx.xxx.xxx-L <br>
  134. * El DNI ha de tener longitud 12
  135. *
  136. * @param dni String con DNI a ser validado
  137. *
  138. * @return true, si el DNI cumple el estandar nacional.
  139. * FECHA: Enero 2016
  140. * AUTOR: Miguel Garcia
  141. * @throws ErrorMessages
  142. *
  143. * **************************************************************************************/
  144. public static boolean cumpleDNI(String dni) throws ErrorMessages{
  145. boolean res = false;
  146. if(dni.length()==LONGITUD_DNI){
  147. String letra = dni.substring(dni.length()-1);
  148. String numeros = dni.substring(0,dni.length()-2 );
  149. String dniSinPuntos = numeros.replace(".", "");
  150. int losNumeros = Integer.parseInt(dniSinPuntos);
  151. System.out.println(dniSinPuntos);
  152. int numero =0;
  153. numero = losNumeros % 23;
  154. String dniNew = LETRA_DNI.substring(numero, numero+1);
  155. if(dniNew.equals(letra))
  156. res = dni.matches(DNI_PATTERN);
  157. }
  158. else
  159. throw new ErrorMessages("DNI CON lONGUITUD EXCEDIDO");
  160. return res;
  161. }
  162.  
  163.  
  164. /** ***************************************************************************************
  165. * NOMBRE: cumpleRango *
  166. *
  167. * DESCRIPCI�N: *//**
  168. * Comprueba que un N�mero se necuentra entre 2 valores
  169. *
  170. * @param valor (int)/(double) N�mero a comprobar
  171. * @param valorMinimo (int) N�mero valor aceptable
  172. * @param valorMaximo (int) M�N�mero valor aceptable
  173. *
  174. * @return true si valorMinimo > valor > valorMaximo
  175. * FECHA: Enero 2016
  176. * AUTOR: Miguel Garcia
  177. *
  178. * **************************************************************************************/
  179. public static boolean cumpleRango(
  180. int valor,
  181. int valorMinimo,
  182. int valorMaximo){
  183.  
  184. return valor >= valorMinimo && valor <= valorMaximo;
  185.  
  186. }
  187.  
  188.  
  189. /* ***************************************************************************************
  190. * NOMBRE: cumpleLongitudMin *
  191. *
  192. * DESCRIPCI�N: *//**
  193. * Comprobar que el texto pasado tiene una longitud de al menos x caracteres, siendo
  194. * x el entero pasado como par�metro
  195. *
  196. * @param texto String texto a comprobar
  197. * @param longitudMinima int que indique longitud m�nima.
  198. *
  199. * @return cierto, si la longitud del texto es mayor o igual que longitudMinima
  200. * FECHA: Enero 2016
  201. * AUTOR: Miguel Garcia
  202. *
  203. * **************************************************************************************/
  204. public static boolean cumpleLongitudMin(
  205. String texto,
  206. int longitudMinima){
  207. return texto != null && texto.length() >= longitudMinima;
  208. }
  209.  
  210.  
  211. /* ***************************************************************************************
  212. * NOMBRE: cumpleLongitudMax *
  213. *
  214. * DESCRIPCI�N: *//**
  215. * Comprobar que el texto pasado tiene una longitud de, como mucho, x caracteres, siendo
  216. * x el entero pasado como par�metro
  217. *
  218. * @param texto String con el texto a comprobar
  219. * @param longitudMaxima int con la longitud m�xima del texto
  220. *
  221. * @return true, si el texto es menor o igual que la longitud m�xima.
  222. * FECHA: Enero 2016
  223. * AUTOR: Miguel Garcia
  224. *
  225. * **************************************************************************************/
  226. public static boolean cumpleLongitudMax(
  227. String texto,
  228. int longitudMaxima){
  229. return texto.length() <= longitudMaxima;
  230. }
  231.  
  232.  
  233. /****************************************************************************************
  234. * NOMBRE: cumpleLongitud *
  235. *
  236. * DESCRIPCI�N: *//**
  237. * Comprobar que la longitud de un texto se encuentra entre unos valores m�ximos y m�nimos
  238. *
  239. * @param texto String con el texto que a validar
  240. * @param longitudMinima (int) M�nima longitud del texto
  241. * @param longitudMaxima (int) M�xima longitud v�lida para el texo
  242. *
  243. * @return true, si la longitud del texto cumple: longitudMinima
  244. * <= longitudTexto <=longitudMaxima
  245. * FECHA: Enero 2016
  246. * AUTOR: Miguel Garcia - Barcelona
  247. *
  248. * **************************************************************************************/
  249. public static boolean cumpleLongitud(
  250. String texto,
  251. int longitudMinima,
  252. int longitudMaxima){
  253.  
  254. return cumpleLongitudMin(texto,longitudMinima) && cumpleLongitudMax(texto,longitudMaxima);
  255.  
  256. }
  257. /**
  258. * Valida una fecha calendar con m�nimo min
  259. * @param fecha
  260. * @param min
  261. * @return
  262. */
  263.  
  264. public static boolean valDateMin(Calendar fecha, Calendar min){
  265. return fecha.after(min);
  266. }
  267.  
  268. /**
  269. * Valida una fecha calendar con m�ximo max
  270. * @param fecha
  271. * @param max
  272. * @return
  273. */
  274. public static boolean valDateMax(Calendar fecha, Calendar max){
  275. return fecha.before(max);
  276. }
  277.  
  278. /**
  279. * esFechaValida
  280. * Recibe una string con formato fecha dd/mm/aaaa y comprueba el formato
  281. * @param fecha
  282. * @return
  283. * @throws ParseException
  284. */
  285. public static boolean esFechaValida(String fecha) throws ParseException{
  286. try {
  287. DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
  288. df.setLenient(false); // this is important!
  289. df.parse(fecha);
  290. }
  291. catch (ParseException e) {
  292. return false;
  293. }
  294. return true;
  295. }
  296.  
  297.  
  298. /**
  299. * Nombre esPasswordValida
  300. * Descripcion Comprueba que la cadena recibida cumpla con lasnormas de contrase�a
  301. * @param password string con la contrase�a introducida
  302. * @return true si cumple con las especificaciones
  303. */
  304. public static boolean esPasswordValida(String password){
  305. boolean res=false;
  306. if(password.length()>=LONG_MIN_PASS && password.length()<=LONG_MAX_PASS)
  307. res= password.matches(PASSWORD_PATTERN);
  308. return res;
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement