Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

bmattos

Formatar número em moeda

Recommended Posts

O seguinte código formata um numeral ao digitar corretamente, entretanto ele não mantém a disposição das casas decimais caso um número seja removido dentro de uma suquencia.

 

Por exemplo, se eu remover o 5 no número 44.544,00 a disposição das casas decimais fica errada.

 

Algum colega sabe o que falta ajustar nesse código ?

 

<html><head><title>teste</title><script language="JavaScript">function FormataValor(campo,tammax,teclapres) {var tecla = teclapres.keyCode;vr = document.form[campo].value;vr = vr.replace( "/", "" );vr = vr.replace( "/", "" );vr = vr.replace( ",", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );tam = vr.length;if (tam < tammax && tecla != 8){ tam = vr.length; }if (tecla == 8 ){ tam = tam - 1; }if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){if ( tam <= 2 ){document.form[campo].value = vr; }if ( (tam > 2) && (tam <= 5) ){document.form[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 6) && (tam <= 8) ){document.form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 9) && (tam <= 11) ){document.form[campo].value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 12) && (tam <= 14) ){document.form[campo].value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 15) && (tam <= 17) ){document.form[campo].value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );}}}</script></head><body><form method="POST" name="form"><p><input type="Text" name="valor" size="23" maxlength="17" onKeyUp="FormataValor('valor', 13, event)"></p></form></body></html>

Muito Obrigado.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Cara não sei ajustar, mas tenho uma exemplo que eu curto muito. Vou postar se te agradar:

<script language="javascript">
function MascaraMoeda(objTextBox, 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;
	if (whichCode == 13) 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;
	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;
}
</script>
Aí no Evento do Botão:

onKeyPress="return(MascaraMoeda(this,'.',',',event))"

Compartilhar este post


Link para o post
Compartilhar em outros sites

Essa máscara não aceita números negativos e quando trabalhamos com moeda números deste tipo fazem parte do contexto!! Único prob dela!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Cara não sei ajustar, mas tenho uma exemplo que eu curto muito. Vou postar se te agradar:

<script language="javascript">function MascaraMoeda(objTextBox, 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;	if (whichCode == 13) 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;	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;}</script>
Aí no Evento do Botão:
onKeyPress="return(MascaraMoeda(this,'.',',',event))"
Olá amigo, esse código não está funcionando perfeitamente no Firefox. Não está permitindo o "Backspace". Como concertar esse problema?

Compartilhar este post


Link para o post
Compartilhar em outros sites

pessoal tenho essa solução, consegui a maior parte do codigo na net mesmo... fiz alguns ajustes e tem funcionado bem nos meus sistemas e funciona tambem no firefox.

 


function formataValor(campo) {
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;
	if ( tam <= 2 ){
 		campo.value = vr ; }
 	if ( (tam > 2) && (tam <= 5) ){
 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 6) && (tam <= 8) ){
 		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 9) && (tam <= 11) ){
 		campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 12) && (tam <= 14) ){
 		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 15) && (tam <= 18) ){
 		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
}

function filtraCampo(campo){
	var s = "";
	var cp = "";
	var CaracValidos = /[-0123456789]/;
	vr = campo.value;
	tam = vr.length;
	for (i = 0; i < tam ; i++) {
		if(CaracValidos.test(vr.substring(i,i+1))){
		 	s = s + vr.substring(i,i + 1);}
	}
	campo.value = s;
   return cp = campo.value;
}

para usar é so add no campo do formulario..

 

<input name="valor" type="text" id="valor" onkeyup="formataValor(this);" />

espero que seja útil pra vocês... ;)

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.