Skip to content

Instantly share code, notes, and snippets.

@donpandix
Last active March 18, 2024 23:22
Show Gist options
  • Save donpandix/fe7def5915ec0a78682aff3deecb2656 to your computer and use it in GitHub Desktop.
Save donpandix/fe7def5915ec0a78682aff3deecb2656 to your computer and use it in GitHub Desktop.
Valida RUT con Java
/**
* Validación de RUT Chileno
* algoritmo Modulo 11
*/
public class CommonFn {
/**
* Valida rut de la forma XXXXXXXX-X
*/
public static Boolean validaRut ( String rut ) {
Pattern pattern = Pattern.compile("^[0-9]+-[0-9kK]{1}$");
Matcher matcher = pattern.matcher(rut);
if ( matcher.matches() == false ) return false;
String[] stringRut = rut.split("-");
return stringRut[1].toLowerCase().equals(CommonFn.dv(stringRut[0]));
}
/**
* Valida el dígito verificador
*/
public static String dv ( String rut ) {
Integer M=0,S=1,T=Integer.parseInt(rut);
for (;T!=0;T=(int) Math.floor(T/=10))
S=(S+T%10*(9-M++%6))%11;
return ( S > 0 ) ? String.valueOf(S-1) : "k";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment