Skip to content

Instantly share code, notes, and snippets.

@betray32
Last active June 5, 2019 22:38
Show Gist options
  • Save betray32/a25d8c55fc59c78a8ed5b8ed428c19f6 to your computer and use it in GitHub Desktop.
Save betray32/a25d8c55fc59c78a8ed5b8ed428c19f6 to your computer and use it in GitHub Desktop.
Validador rut chileno
/**
* 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