Last active
June 5, 2019 22:38
-
-
Save betray32/a25d8c55fc59c78a8ed5b8ed428c19f6 to your computer and use it in GitHub Desktop.
Validador rut chileno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Validar rut chileno | |
* | |
* @param rut | |
* @return | |
*/ | |
public static boolean validarRut(String rut) { | |
boolean validacion = false; | |
try { | |
rut = rut.toUpperCase(); | |
rut = rut.replace(".", ""); | |
rut = rut.replace("-", ""); | |
int rutAux = Integer.parseInt(rut.substring(0, rut.length() - 1)); | |
char dv = rut.charAt(rut.length() - 1); | |
int m = 0, s = 1; | |
for (; rutAux != 0; rutAux /= 10) { | |
s = (s + rutAux % 10 * (9 - m++ % 6)) % 11; | |
} | |
if (dv == (char)(s != 0 ? s + 47 : 75)) { | |
validacion = true; | |
} | |
} catch (java.lang.NumberFormatException e) { | |
log.error("Rut ingresado incorrecto > " + rut); | |
} catch (Exception e) { | |
log.error("Rut ingresado incorrecto > " + rut); | |
} | |
return validacion; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment