Cambios js
Idea general: para que los DNI y CUIL lleguen al visor con sus formatos correspondientes lo que hice fue crear 2 funciones que den el
Función formato_CUIL
//============================================================
// FUNCION FORMATO CUIL
//============================================================
function formato_CUIL(num){
var aux = num.indexOf('-') === -1? num : deformat(num);
return aux.substring(0,2) + '-' + aux.substr(2,aux.length-2-1) + '-' + aux[aux.length -1];
}
# formato_CUIL(in: num, out: res)
- Pre: {}
- Post: { res es igual a num pero con un guión después de los dos primeros strings de num y antes del último char de num }
Función formato_DNI
//============================================================
// FUNCION FORMATO DNI
//============================================================
function formato_DNI(num){
var aux = num.indexOf('.') === -1? num : deformat(num);
var cantPuntos = Math.floor(aux.length/3);
var dni = '';
if(aux.length%3 != 0) { // Tomo los primeros 1 o 2 numeros
dni = aux.substring(0, num.length%3) + '.';
aux = aux.substring(num.length%3, num.length) // Recorto esos numeros que tome
}
for(var i = 0 ; i < cantPuntos ; i++){
dni += aux.substring(0,3);
if(i != cantPuntos-1) dni += '.';
aux = aux.substring(3, aux.length);
}
return dni;
}
# formato_DNI(in: num, out: res)
- Pre: {}
- Post: { res es igual a num pero tiene un punto por cada tres digitos de la derecha hacia la izquierda }
Función deformat
//============================================================
// FUNCIÓN PARA BORRAR FORMATO (CUIL/DNI)
//============================================================
function deformat(str){
var numeros = [];
for (var i = 0; i < str.length; i++) {
// Recorro el string str y guardo solo los numeros
if (/[0-9]/.test(str[i])) numeros.push(str[i]);
}
return numeros.join("");
//return noCommas(noDashes(noDots(str)));
}
# deformat(in: str, out: res)
- Pre: {}
- Post: {Dado un string str, retorna un string res en el que solo estarán presentes los chars que representen números}
- Obs: La idea de uso de esta función es hacer un control cruzado del formato del string que entra a formato_DNI o formato_CUIL por l
var aux = num.indexOf('.') === -1? num : deformat(num); // Formato DNI
var aux = num.indexOf('-') === -1? num : deformat(num); // Formato CUIL