Last active
March 18, 2024 23:22
-
-
Save donpandix/fe7def5915ec0a78682aff3deecb2656 to your computer and use it in GitHub Desktop.
Valida RUT con Java
This file contains hidden or 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
/** | |
* 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