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.
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.
y no hubiera sido mejor un REGEX? se me ocurre algo como ^([0-9]{5,})-(k|[0-9]{1})$
mmm al principio creí que solo verificaban el patrón, pero veo que el ultimo numero si tiene su razón de ser.
Gracias por el aporte, ya lo prove en un proyecto y si me validó el NIT, gracias
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;
}
}
}
}
Mismo Código convertido a Visual Basic.
”’
”’ Función para verificar el Nit Ingresado.
”’
”’ Nit a Verificar
”’ True o False
”’ Verifica si el nit ingresado tiene forma correcta.
Public Function Valida_Nit_303(ByVal NIT As String) As Boolean
Dim POS As Integer
Dim Correlativo As String
Dim DigitoVerificador As String
Dim Factor As Integer
Dim Suma As Integer = 0
Dim Valor As Integer = 0
Dim X As Integer
Dim xMOD11 As Double = 0
Dim S As String = “”
Valida_Nit_303 = False
Try
POS = NIT.IndexOf(“-”)
If POS = 0 Then Exit Function
Correlativo = NIT.Substring(0, POS)
DigitoVerificador = NIT.Substring(POS + 1)
Factor = Correlativo.Length + 1
For X = 0 To (NIT.IndexOf(“-”) – 1)
Valor = Convert.ToInt32(NIT.Substring(X, 1))
Suma += (Valor * Factor)
Factor -= 1
Next
xMOD11 = (11 – (Suma Mod 11)) Mod 11
S = Convert.ToString(xMOD11)
If (xMOD11 = 10 And DigitoVerificador = “K”) Or (S = DigitoVerificador) Then
Valida_Nit_303 = True
End If
Catch ex As Exception
MsgBox(ex.toString)
End Try
End Function
Gracias por el aporte Saludos