Warning: fsockopen() [function.fsockopen]: unable to connect to widget.seocontentsolutions.com:80 (No route to host) in /home/at000150/public_html/wp-includes/class-snoopy.php on line 1142
Validar un NIT(Guatemala) en C# | GeoAvila (Geovanny G. Avila)

GeoAvila (Geovanny G. Avila) Desarrollo.. de software.

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#:
  1. public bool ValidarNIT(string Nit)
  2.         {
  3.             int pos = Nit.IndexOf("-");
  4.             string Correlativo = Nit.Substring(0, pos);
  5.             string DigitoVerificador = Nit.Substring(pos + 1);
  6.             int Factor = Correlativo.Length + 1;
  7.             int Suma = 0;
  8.             int Valor = 0;
  9.  
  10.             for (int x = 0; x <= Nit.IndexOf("-") - 1; x++)
  11.             {
  12.                 Valor = Convert.ToInt32(Nit.Substring(x, 1));
  13.                 Suma = Suma + (Valor * Factor);
  14.                 Factor = Factor - 1;
  15.             }
  16.  
  17.             double xMOd11 = 0;
  18.             xMOd11 = (11 - (Suma % 11)) % 11;
  19.             string s = Convert.ToString(xMOd11);
  20.             if ((xMOd11 == 10 & DigitoVerificador == "K") | (s == DigitoVerificador))
  21.             {
  22.                 return true;
  23.             }
  24.             return false;
  25.         }

Nos Vemos.

No TweetBacks yet. (Be the first to Tweet this post)
Comments (4) Trackbacks (0)
  1. y no hubiera sido mejor un REGEX? se me ocurre algo como ^([0-9]{5,})-(k|[0-9]{1})$

  2. mmm al principio creí que solo verificaban el patrón, pero veo que el ultimo numero si tiene su razón de ser.

  3. Gracias por el aporte, ya lo prove en un proyecto y si me validó el NIT, gracias

  4. 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;
    }

    }

    }
    }


Leave a comment


No trackbacks yet.

Sigueme en Twitter

Recent Posts

Amigos de Twitter

isagc_88
pena2
kalugt
mishu7
luismuybien
evesosa
prsarahevans
PilarPreza
valeriaau
thomas_weller
xabivargas
albertomontt
J05uE
softcarlos
Blackstare
guise
fayerwayer
ChapinGeek
yoasumo
LibrosyMasgt
jazzandblue
daniellacayo
xisaac
orlandomgj
guilleybruno
TuFrac
Luckysilva
guatecham
f0vela
barcampgt
Elultimodepaz
tuxtor
giopack
xarquis
andreimorales
robertogt
Friends: Followers:

Categories

Sitios amigos

Algunos Derechos Reservados

Blog bajo licencia Creative Commons Attribution-ShareAlike 3.0 License
Creative Commons License