function validateInteger(element, max, e){ var teclachar; var whichCode = (window.Event) ? e.which : e.keyCode; if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8)) return true; teclachar = String.fromCharCode(whichCode); if (teclachar <'0' || teclachar > '9') return false; if (element.value.length >= max) return false; return true; } function validadeNumber(objTextBox, maxLength, SeparadorMilesimo, SeparadorDecimal, e){ var sep = 0; var key = ''; var i = j = 0; var len = len2 = 0; var strCheck = '0123456789'; var aux = aux2 = ''; var whichCode = (window.Event) ? e.which : e.keyCode; // 13=enter, 8=backspace as demais retornam 0(zero) // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8)) return true; key = String.fromCharCode(whichCode); // Valor para o código da Chave if (strCheck.indexOf(key) == -1) return false; // Chave inválida len = objTextBox.value.length; if(len >= maxLength) return false; for(i = 0; i < len; i++) if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break; aux = ''; for(; i < len; i++) if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i); aux += key; len = aux.length; if (len == 0) objTextBox.value = ''; if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux; if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux; if (len > 2) { aux2 = ''; for (j = 0, i = len - 3; i >= 0; i--) { if (j == 3) { aux2 += SeparadorMilesimo; j = 0; } aux2 += aux.charAt(i); j++; } objTextBox.value = ''; len2 = aux2.length; for (i = len2 - 1; i >= 0; i--) objTextBox.value += aux2.charAt(i); objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len); } return false; } function roundNumber(num, dec) { var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); return result; } function onKeyPressInputMoney(event, target) { return onlyNumbers(event, target); } function onlyNumbers(event, target) { var keyCode; var keyChar; if (window.event) { keyCode = window.event.keyCode; } else if (event) { keyCode = event.which; } else { return true; } keyChar = String.fromCharCode(keyCode); // teclas de controle if ((keyCode == null) || (keyCode == 0) || (keyCode == 8) || (keyCode == 9) || (keyCode == 13) || (keyCode == 27)) { return true; } else if ((("0123456789").indexOf(keyChar) > -1)) { return true; } else { return false; } } function onKeyUpInputMoney(event, target) { var keyCode; var keyChar; if (window.event) { keyCode = window.event.keyCode; } else if (event) { keyCode = event.which; } else { return; } keyChar = String.fromCharCode(keyCode); if ((keyCode == 8) || (keyCode == 46) || (keyCode >= 96 && keyCode <= 105) || (("0123456789").indexOf(keyChar) > -1)) { target.value = currencyFormatted(target.value); } } function currencyFormatted(amount) { var formatedValue = amount; var real = ''; var cents = ''; var temp = []; var i = 0; var j = 0; var k = 0; formatedValue = clearString(formatedValue.toString(), "0123456789"); if(formatedValue.length > 2) { real = formatedValue.substr(0, formatedValue.length - 2); cents = formatedValue.substr(formatedValue.length - 2, 2); if(real.length > 3) { temp = []; for(i = real.length - 1, j = 1, k = 0; i > 0 ; i--, j++) { if((j % 3) == 0) { temp.push(real.substr(i, 3)); k++; } } temp.reverse(); real = real.substr(0, real.length - (3 * k))+'.'+temp.join('.'); } formatedValue = real + ',' + cents; } return formatedValue; } function clearString(value, validCharacters) { var result = ''; var index = -1; var i = 0; for(i = 0; i < value.length; i++) { index = validCharacters.indexOf(value.charAt(i)); if(index > -1) { result += validCharacters.charAt(index); } } return result; } function onBlurInputMoney(event, target) { target.value = currencyFormatted(target.value); if(target.value.length == 0) { target.value = ''; } else if(target.value.length <= 2) { target.value = target.value + ',00'; } } $(function (){ $(".inputCurrency").each(function(){ if($(this).val() == "") $(this).val("0,00"); }); $(".inputCurrency").blur(function(e){ onBlurInputMoney(e, this); if($(this).val() == "") $(this).val("0,00"); }); $(".inputCurrency").keyup(function(e){ onKeyUpInputMoney(e, this); }); $(".inputCurrency").keypress(function(e){ return onKeyPressInputMoney(e, this); }); $(".inputCurrency").click(function(){ if($(this).val() == "0,00") $(this).val(""); }); $(".somVlr#").blur(function (){ var pag = 0; $(".somVlr").each(function (){ pag += parseFloat(replaceNumber($(this).val()).replace(',','.')); }); pag = currencyFormatted(pag); $(".somTtl").val(pag); }); /*$(".txtQtdTit").blur(function (){ var pag = 0; $(".txtQtdTit").each(function (){ pag += parseInt($(this).val()); }); pag *= 10000; pag = currencyFormatted(pag); $(".somTtl").text(pag); });*/ }); function replaceNumber(num){ var arrNum; if(num != ""){ if(num.indexOf('.') > 0){ arrNum = num.split("."); num = arrNum.join(''); } } return num; }