andgau

Validación NIF

Jan 31st, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1.     public static boolean validarNIF(String nif) {
  2.         boolean correcto = false;
  3.         Pattern pattern = Pattern
  4.                 .compile("(\\d{1,8})([TRWAGMYFPDXBNJZSQVHLCKEtrwagmyfpdxbnjzsqvhlcke])");
  5.         Matcher matcher = pattern.matcher(nif);
  6.         if (matcher.matches()) {
  7.             String letra = matcher.group(2);
  8.             String letras = "TRWAGMYFPDXBNJZSQVHLCKE";
  9.             int index = Integer.parseInt(matcher.group(1));
  10.             index = index % 23;
  11.             String reference = letras.substring(index, index + 1);
  12.             correcto =reference.equalsIgnoreCase(letra);   
  13.         }
  14.         return correcto;
  15.     }
Advertisement
Add Comment
Please, Sign In to add comment