Forma de validar un nit en colombia usando el digito de verificación.
El algoritmo se llama modulo 11, y consiste en coger todo el nit y separar cada digito, se multiplica con el respectivo numero primo, al finalizar se realizan las sumas de los productos de cada digito y el acumulado se divide en 11, si el residuo es 0 ó 1 el d.v es ese residuo, si es diferente, a 11 se le resta ese residuo y el resultado es el d.v.
TSQL - ORACLE:
function(){ CREATE OR REPLACE FUNCTION ValidarDigitoChequeoNIT(chrNIT VARCHAR2) RETURN VARCHAR2 IS BEGIN DECLARE intContador NUMBER; intACUM NUMBER; intRESTO NUMBER; intLongIden NUMBER; --Longitud de la cadena identificación} -- Arreglo TYPE arregloPA IS TABLE OF NUMBER(2) INDEX BY BINARY_INTEGER; -- Variable de tipo arreglo arrPA arregloPA; arrDA arregloPA; BEGIN -- Válida el dígito de chequeo SELECT Length(chrNIT) + 1 INTO intLongIden FROM DUAL; -- Llenar el arreglo PA, estos son números primos, según loe stablece el algoritmo arrPA(1) := 3; arrPA(2) := 7; arrPA(3) := 13; arrPA(4) := 17; arrPA(5) := 19; arrPA(6) := 23; arrPA(7) := 29; arrPA(8) := 37; arrPA(9) := 41; arrPA(10) := 43; arrPA(11) := 47; arrPA(12) := 53; arrPA(13) := 59; arrPA(14) := 67; arrPA(15) := 71; intContador := 1; -- Llena el arreglo DA con la cadena de entrada que es el NIT FOR intContador IN 1..intLongIden - 1 LOOP arrDA(intContador) := TO_NUMBER(substr(chrNIT,intLongIden- intContador, 1)); END LOOP; -- Llena con ceroas el resto de posiciones del arreglo FOR intContador IN intLongIden..15 LOOP arrDA(intContador) := 0; END LOOP; intACUM := 0; FOR intContador IN 1..15 LOOP intACUM := intACUM + (arrDA(intContador) * arrPA(intContador)); END LOOP; -- Obtiene el modulo 11 del acumulado intRESTO := MOD(intACUM,11); IF (intRESTO > 1) THEN return TO_CHAR(11 - intRESTO); ELSE return TO_CHAR(intRESTO); END IF; END; END F_ValidarDigitoChequeoNIT;
C-SHARP C#:
public static string NitValidarDigitoVerificacion(string unNit)
{
string miTemp;
int miContador;
int miResiduo;
int miChequeo;
int[] miArregloPA = new int[15];
miArregloPA[0] = 3;
miArregloPA[1] = 7;
miArregloPA[2] = 13;
miArregloPA[3] = 17;
miArregloPA[4] = 19;
miArregloPA[5] = 23;
miArregloPA[6] = 29;
miArregloPA[7] = 37;
miArregloPA[8] = 41;
miArregloPA[9] = 43;
miArregloPA[10] = 47;
miArregloPA[11] = 53;
miArregloPA[12] = 59;
miArregloPA[13] = 67;
miArregloPA[14] = 71;
miChequeo = 0;
miResiduo = 0;
for (miContador = 0; miContador<unNit.Length;miContador++)
{
miTemp = unNit[(unNit.Length-1) - miContador].ToString();
miChequeo = miChequeo + (Convert.ToInt32(miTemp) * miArregloPA[miContador]);
}
miResiduo = miChequeo % 11;
if (miResiduo > 1)
return Convert.ToString(11 - miResiduo);
return miResiduo.ToString();
}
VISUAL FOXPRO
select 6 if trim(tempnit.tipo) = "NIT" d = 0 digito1 = "41372923191713 7 3" nit1=substr(str(nit),2,9) nnit=" " N=1 do while n<=len(nit1) d = d + val(trim(substr(digito1,n*2-1,2)))*val(substr(nit1,n,1)) n=n+1 enddo d=mod(d,11) if d > 1 d = 11 - d endif d=substr(str(d),10,1) nnit=nit1+"-"+d replace tempnit.digito with d thisform.pageframe1.page2.text6.visible = .t. thisform.pageframe1.page2.text6.refresh() thisform.pageframe1.page2.label8.visible = .t. thisform.pageframe1.page2.text6.refresh() else replace tempnit.digito with " " endif
VISUAL BASIC
FUNCTION Nit_DV(_Nit) LOCAL _TipoRet, lnRetorno, Arreglo_PA, WSuma, WDato, WDig_Ver, I _TipoRet = VARTYPE(_Nit) DO CASE CASE _TipoRet == "C" _Nit = ALLTRIM(_Nit) CASE _TipoRet == "N" OR _TipoRet == "Y" _Nit = ALLTRIM(STR(_Nit)) OTHERWISE =MESSAGEBOX("El valor de entrada NIT no se ha podido procesar.",0+48,_SCREEN.cNomApp) RETURN ENDCASE DIMENSION Arreglo_PA(15) Arreglo_PA(1) = 71 Arreglo_PA(2) = 67 Arreglo_PA(3) = 59 Arreglo_PA(4) = 53 Arreglo_PA(5) = 47 Arreglo_PA(6) = 43 Arreglo_PA(7) = 41 Arreglo_PA(8) = 37 Arreglo_PA(9) = 29 Arreglo_PA(10) = 23 Arreglo_PA(11) = 19 Arreglo_PA(12) = 17 Arreglo_PA(13) = 13 Arreglo_PA(14) = 7 Arreglo_PA(15) = 3 lnRetorno = 0 WDato=RIGHT(SPACE(15)+ALLTRIM(_Nit),15) WSuma=0 WDig_Ver=0 FOR I = 1 TO 15 WSuma=WSuma+(VAL(SUBSTR(WDato,I,1))*Arreglo_PA(I)) ENDFOR WSuma=MOD(WSuma,11) IF(WSuma=0 .OR. WSuma=1) lnRetorno = WSuma ELSE lnRetorno = 11 - WSuma ENDIF IF _TipoRet == "C" RETURN ALLTRIM(STR(lnRetorno)) ELSE RETURN lnRetorno ENDIF ENDFUNC
JAVA
public byte generarDv(long nit) { int[] nums = { 3, 7, 13, 17, 19, 23, 29, 37, 41, 43, 47, 53, 59, 67, 71 }; int sum = 0; String str = String.valueOf(nit); for (int i = str.length() - 1, j=0; i >= 0; i--, j++) { suma += Character.digit(str.charAt(i), 10) * nums[j]; } byte dv = (byte)((sum % 11) > 1 ? (11 - (sum % 11)) : (sum % 11)); return dv; }
DELPHI
function DigVerNIT(NIT : String) : Boolean; const Pesos: array[0..11] of integer=(53,47,43,41,37,29,23,19,17,13,7,3); var cont : Integer; suma : Integer; DV : Integer; begin Result := False; suma := 0; NIT := AlineaD(NIT,12,'0'); For cont := 0 to 11 do suma := suma + StrToInt(Copy(NIT, cont, 1)) * Pesos[cont]; DV := 11 - suma mod 11; if (DV > 9) then DV:= 1 - DV mod 10; if StrToInt(Copy(NIT, 12, 1)) = DV then Result := True; end;
EXCEL
*El numero del nit debe estar en la celda C1 * En la celda E1 colocas lo siguiente "=SI(F1=0,0,SI(F1=1,1,11-F1))" Sin las comillas * Y en la celda F1 Colocas esto "=RESIDUO((VALOR(EXTRAE(TEXTO(C1,"000000000000000" ),15,1))*3+VALOR(EXTRAE(TEXTO(C1,"000000000000000" ),14,1))*7+VALOR(EXTRAE(TEXTO(C1,"000000000000000" ),13,1))*13+VALOR(EXTRAE(TEXTO(C1,"000000000000000 "),12,1))*17+VALOR(EXTRAE(TEXTO(C1,"00000000000000 0"),11,1))*19+VALOR(EXTRAE(TEXTO(C1,"0000000000000 00"),10,1))*23+VALOR(EXTRAE(TEXTO(C1,"000000000000 000"),9,1))*29+VALOR(EXTRAE(TEXTO(C1,"000000000000 000"),8,1))*37+VALOR(EXTRAE(TEXTO(C1,"000000000000 000"),7,1))*41+VALOR(EXTRAE(TEXTO(C1,"000000000000 000"),6,1))*43+VALOR(EXTRAE(TEXTO(C1,"000000000000 000"),5,1))*47+VALOR(EXTRAE(TEXTO(C1,"000000000000 000"),4,1))*53+VALOR(EXTRAE(TEXTO(C1,"000000000000 000"),3,1))*59+VALOR(EXTRAE(TEXTO(C1,"000000000000 000"),2,1))*67+VALOR(EXTRAE(TEXTO(C1,"000000000000 000"),1,1))*71),11)" Sin las comillas
JAVASCRIPT
<script type="text/javascript">
function zero_fill(i_valor, num_ceros) {
relleno = "";
i = 1;
salir = 0;
while ( ! salir ) {
total_caracteres = i_valor.length + i;
if ( i > num_ceros || total_caracteres > num_ceros )
salir = 1;
else
relleno = relleno + "0";
i++;
}
i_valor = relleno + i_valor;
return i_valor;
}
function calcularDV(i_rut) {
var pesos = new Array(71,67,59,53,47,43,41,37,29,23,19,17,13,7,3);
rut_fmt = zero_fill(i_rut, 15);
suma = 0;
for ( i=0; i<=14; i++ )
suma += rut_fmt.substring(i, i+1) * pesos[i];
resto = suma % 11;
if ( resto == 0 || resto == 1 )
digitov = resto;
else
digitov = 11 - resto;
return(digitov);
}
function validarNit(){
var nit = document.getElementById("nit").value;
if(nit.length<10){alert("Identificación NIT NO valido, digite los 10 digitos, incluido digito de verificación ");return false;}
if(isNaN(nit)){alert("Identificación NIT NO valido, digite los 10 digitos, incluido digito de verificación ");return false;}
var divc = calcularDV(nit.substr(0,9)); var divnit = nit.substr(9); //alert(divc); alert(divnit);
if( divc != divnit ){alert("Identificación NIT NO valido, digite los 10 digitos, incluido digito de verificación ");return false;}
else {return true;}
}
</script>
OPENOFFICE
Como ejemplo vamos a utilizar la primera fila de las columnas: "A", "B", "D", por lo tanto... "A1" Donde digitamos las cédulas "B1" Donde visualizaremos los DVs "D1" Donde se "calcularán" una parte los datos. 1. Copiamos y pegamos la siguiente fórmula en la celda "B1" =SI(D1=0;0;SI(D1=1;1;11-D1)) 2. Copiamos y pegamos la siguiente fórmula en la celda "D1" =RESIDUO((VALOR(EXTRAE(TEXTO(A1;"000000000000000") ;15;1))*3+VALOR(EXTRAE(TEXTO(A1;"000000000000000") ;14;1))*7+VALOR(EXTRAE(TEXTO(A1;"000000000000000") ;13;1))*13+VALOR(EXTRAE(TEXTO(A1;"000000000000000" );12;1))*17+VALOR(EXTRAE(TEXTO(A1;"000000000000000 ");11;1))*19+VALOR(EXTRAE(TEXTO(A1;"00000000000000 0");10;1))*23+VALOR(EXTRAE(TEXTO(A1;"0000000000000 00");9;1))*29+VALOR(EXTRAE(TEXTO(A1;"0000000000000 00");8;1))*37+VALOR(EXTRAE(TEXTO(A1;"0000000000000 00");7;1))*41+VALOR(EXTRAE(TEXTO(A1;"0000000000000 00");6;1))*43+VALOR(EXTRAE(TEXTO(A1;"0000000000000 00");5;1))*47+VALOR(EXTRAE(TEXTO(A1;"0000000000000 00");4;1))*53+VALOR(EXTRAE(TEXTO(A1;"0000000000000 00");3;1))*59+VALOR(EXTRAE(TEXTO(A1;"0000000000000 00");2;1))*67+VALOR(EXTRAE(TEXTO(A1;"0000000000000 00");1;1))*71);11) Si desea utilizar una celda diferente para introducir las cédulas, basta con cambiar las referencias "A1" (sin las comillas) de la formula anterior, que es donde ubicamos las cédulas. Y asi mismo con la fórmula del paso #2 hay que cambiar "D1" por por la celda donde ubicó la fórmula del paso #2.
PHP
function zero_fill($nit) { $relleno = ''; for ($i=1; $i < 15; $i++) { $total_char = strlen($nit) + $i; if ($total_char <= 15) $relleno .= "0"; else break; } $nit = $relleno.$nit; return $nit; } function calcularDV($nit) { $pesos = array(71,67,59,53,47,43,41,37,29,23,19,17,13,7,3); $nit_fmt = zero_fill($nit); $suma = 0; for ($i=0; $i<=14; $i++) $suma += substr($nit_fmt, $i, 1) * $pesos[$i]; $resto = $suma % 11; if ($resto == 0 || $resto == 1) $digitov = $resto; else $digitov = 11 - $resto; echo $digitov; }
PERL
sub calcularDV() { my $nit=$_[0]; $nit=~s/\n$//;#elimina salto de linea al final cuando se prueba por consola my @pesos = (71,67,59,53,47,43,41,37,29,23,19,17,13,7,3); #$nit_fmt = &zero_fill($nit); my $nit_fmt=('0'x(15-length($nit))).$nit; # Agrega ceros a la izquierda hasta completar 15 digitos my $suma = 0; for (my $i=0; $i<=14; $i++){ $suma += substr($nit_fmt, $i, 1) * $pesos[$i]; } my $resto = $suma % 11; my $digitov = ($resto == 0 || $resto == 1)?$resto:11 - $resto; # validacion descrita seguidamente #if ($resto == 0 || $resto == 1){ #$digitov = $resto; #}else{ #$digitov = 11 - $resto; #} print $digitov; } my $numero=<>; &calcularDV($numero);
Comentarios
Añadir nuevo comentario