// Formatear campos numéricos function formatNumber(input, maxNumber) { var num = input.value.replace(/\./g, ''); num = num.replace(/\D/g, '') if (!isNaN(num)) { if (num > maxNumber) { num = num.toString().substring(0, maxNumber.toString().length); if (num > maxNumber) num = num.toString().substring(0, maxNumber.toString().length - 1); } num = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g, '$1.'); num = num.split('').reverse().join('').replace(/^[\.]/, ''); input.value = num; } } // Formatear campos numéricos con decimales function formatNumber2(input, numberOfDigitsBeforeComma, numberOfDigitsAfterComma, completeDecimalsWithZeros) { var num = input.value.replace(/\./g, ','); var numparts = num.split(','); numparts[0] = numparts[0].replace(/\D/g, ''); //Elimina cualquier cosa que no sea un carácter numérico if (numparts[0].length > 1) //Elimina 0 delante de otro carácter numérico if (numparts[0].substr(0, 1) == 0) numparts[0] = numparts[0].substr(1, numparts[0].length - 1); if (numparts[0].length > numberOfDigitsBeforeComma) { //Un bug de Android provoca que al picar 1234 se ponga 12,43!!! if (numparts.length == 1) { // Poner la coma automáticamente numparts = (numparts[0].substr(0,numberOfDigitsBeforeComma) + ',' + numparts[0].substr(numberOfDigitsBeforeComma,1)).split(','); } else { numparts[0] = numparts[0].substr(0,numberOfDigitsBeforeComma); } } if (numparts.length >= 2) { numparts[1] = numparts[1].replace(/\D/g, ''); if (numparts[1].length > numberOfDigitsAfterComma) numparts[1] = numparts[1].substr(0,numberOfDigitsAfterComma); } if (completeDecimalsWithZeros == true) { if (numparts.length >= 2) { if (numparts[0].length == 0) numparts[0] = '0'; numparts[1] = (numparts[1] + '00').substring(0,2); input.value = numparts[0] + ',' + numparts[1]; } else { if (numparts[0].length > 0) input.value = numparts[0] + ',00'; else input.value = '0,00'; } } else { if (numparts.length >= 2) input.value = numparts[0] + ',' + numparts[1]; else input.value = numparts[0]; } } // Formatear campos numéricos con decimales function formatNumber3(input, numberOfDigitsBeforeComma, numberOfDigitsAfterComma, deleteCommaIfNotDecimals) { var num = input.value.replace(/\./g, ','); var numparts = num.split(','); numparts[0] = numparts[0].replace(/\D/g, ''); if (numparts[0].length > numberOfDigitsBeforeComma) numparts[0] = numparts[0].substring(0,numberOfDigitsBeforeComma); if (numparts.length > 1) { numparts[1] = numparts[1].replace(/\D/g, ''); if (numparts[1].length > numberOfDigitsAfterComma) numparts[1] = numparts[1].substring(0,numberOfDigitsAfterComma); if (numparts[1].length == 0 && deleteCommaIfNotDecimals == true) { num = numparts[0]; } else { num = numparts[0] + ',' + numparts[1]; } } else { num = numparts[0]; } input.value = num; }
Blog sobre desarrollo de aplicaciones Software basado en tecnología Microsoft (.Net/SQL/IIS) y otras (php/MySQL/Apache/Android...).