18Dec/094
Validar un NIT(Guatemala) en C#
Esta función, encontre esta función para validar un NIT con JavaScript, bueno pasando a C#, quedaría algo así, espero les sirva el este código.
C#:
-
public bool ValidarNIT(string Nit)
-
{
-
int pos = Nit.IndexOf("-");
-
string Correlativo = Nit.Substring(0, pos);
-
string DigitoVerificador = Nit.Substring(pos + 1);
-
int Factor = Correlativo.Length + 1;
-
int Suma = 0;
-
int Valor = 0;
-
-
for (int x = 0; x <= Nit.IndexOf("-") - 1; x++)
-
{
-
Valor = Convert.ToInt32(Nit.Substring(x, 1));
-
Suma = Suma + (Valor * Factor);
-
Factor = Factor - 1;
-
}
-
-
double xMOd11 = 0;
-
xMOd11 = (11 - (Suma % 11)) % 11;
-
string s = Convert.ToString(xMOd11);
-
if ((xMOd11 == 10 & DigitoVerificador == "K") | (s == DigitoVerificador))
-
{
-
return true;
-
}
-
return false;
-
}
Nos Vemos.
No TweetBacks yet. (Be the first to Tweet this post)





























December 18th, 2009 - 14:35
y no hubiera sido mejor un REGEX? se me ocurre algo como ^([0-9]{5,})-(k|[0-9]{1})$
December 18th, 2009 - 15:01
mmm al principio creí que solo verificaban el patrón, pero veo que el ultimo numero si tiene su razón de ser.
July 22nd, 2010 - 12:42
Gracias por el aporte, ya lo prove en un proyecto y si me validó el NIT, gracias
August 2nd, 2010 - 17:43
hola aca les dejo en C# el algoritmo del NIT para Guatemala, me fue de gran utilidad, ya que me salvo saludos.
namespace NIT
{
public partial class FrmNIT : Form
{
public FrmNIT()
{
InitializeComponent();
}
private void btnValidar_Click(object sender, EventArgs e)
{
if (ValidarNIT(txtNit.Text.ToUpper()))
{
MessageBox.Show("NIT VALIDO: " txtNit.Text);
}
else
{
MessageBox.Show("NIT NO VALIDO: " txtNit.Text);
}
}
public bool ValidarNIT(string Nit)
{
try
{
if (Nit == "") return true;
if (Nit == "CF" || Nit == "C/F") return true;
int pos = Nit.IndexOf("-");
if (pos < 0)
{
string correlativo = Nit.Substring(0, Nit.Length - 1);
correlativo = correlativo "-";
int pos2 = correlativo.Length - 2;
string digito = Nit.Substring(pos2 1);
Nit = correlativo digito;
pos = Nit.IndexOf("-");
}
string Correlativo = Nit.Substring(0, pos);
string DigitoVerificador = Nit.Substring(pos 1);
int Factor = Correlativo.Length 1;
int Suma = 0;
int Valor = 0;
for (int x = 0; x <= Nit.IndexOf("-") - 1; x )
{
Valor = Convert.ToInt32(Nit.Substring(x, 1));
Suma = Suma (Valor * Factor);
Factor = Factor - 1;
}
double xMOd11 = 0;
xMOd11 = (11 - (Suma % 11)) % 11;
MessageBox.Show("Suma: " Suma);
MessageBox.Show("Mod: " xMOd11);
string s = Convert.ToString(xMOd11);
if ((xMOd11 == 10 & DigitoVerificador == "K") | (s == DigitoVerificador))
{
return true;
}
return false;
}
catch (Exception)
{
return false;
}
}
}
}