Created
June 19, 2015 23:14
-
-
Save RicardoACS/4c0507daf2cbea723d3a to your computer and use it in GitHub Desktop.
Validar Rut Chile
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
public bool validarRut(string rut ) { | |
bool validacion = false; | |
try | |
{ | |
rut = rut.ToUpper(); | |
rut = rut.Replace(".", ""); | |
rut = rut.Replace("-", ""); | |
int rutAux = int.Parse(rut.Substring(0, rut.Length - 1)); | |
char dv = char.Parse(rut.Substring(rut.Length - 1, 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 (Exception) | |
{ | |
} | |
return validacion; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment