Método para validación del RUT de Chile.
Los parámetros out long rut y out string dv permiten obtener ambas partes del RUT en cada variable.
///
/// Determina si un rut es válido
///
public static string VerificarRut(string sRut, out long rut, out string dv)
{
rut = 0;
dv = "";
if (string.IsNullOrEmpty(sRut))
return null;
sRut = sRut.Replace("-", "");
sRut = sRut.Replace(".", "");
sRut = sRut.Replace(" ", "");
if(sRut.Length < 2)
return null;
string sdv = sRut.Substring(sRut.Length - 1, 1).ToUpper();
sRut = sRut.Substring(0, sRut.Length - 1);
if(!MyUtil.IsNumeric(sRut))
return null;
int m = 2;
int total = 0;
for(int i = sRut.Length - 1; i >= 0; i--)
{
total = total + Convert.ToInt32(sRut.Substring(i, 1)) * m;
if(m == 7)
m = 1;
m++;
}
int res = 11 - (total % 11);
string dvr = "0";
if(res == 10) {
dvr = "K";
} else if(res == 11) {
dvr = "0";
} else {
dvr = res.ToString();
}
if(sdv.Equals(dvr)) {
rut = Convert.ToInt64(sRut);
dv = sdv;
return sRut + "-" + sdv;
}
return null;
}