Ir para conteúdo

Arquivado

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

rafaelcastro

Máscara valor

Recommended Posts

Grande comunidade

 

Depois de fazer a busca neste fórum, achei um tópico mais ou menos como precisava. Alterei o que precisava e fiz funcionar. Só que agora estou com um detalhe que gostaria de compartilhar, detalhe este que não consigo de forma alguma resolver sozinho...

 

Tenho um campo (field) com a quantidade, outro com o valor unitário e outro com o valor total...

 

O que está acontecendo:

As máscaras nos fields estão ok.

No momento do Submit do form, eu tenho que pegar o valor unitário, dar um replace para retirar os pontos e as virgulas, multiplicar pela quantidade, jogar novamente a mascara e jogar no campo valor total...

 

Eu consigo fazer tudo, multiplico e calculo, mas na hora de voltar pro form com o valor total não aparece nada. Pelo que olhei e entendi talvez esteja convertendo em string e por isso não consegue calcular, mas não tenho certeza

 

Vou postar o código aqui

function calcula()
{ 
		  var pegaquant = document.getElementById("quantidade").value;
		  var valor	 = document.getElementById("val_unitario").value;
		  
		  valor = valor.replace (".","");
		  valor = valor.replace (",","");
		 // valor = valor * pegaquant;

		  var tamanho		= valor.length;
		  var iniciodecimais = tamanho - 2;
		  alert (tamanho);		
		  if(tamanho > 2) 
			 { 
			   var decimais		= valor.substr(iniciodecimais,3);
			   var inteiros		= valor.substr(0,iniciodecimais);
			   var stringinvertida = "";
			   var tamanhoinverte = tamanho - 2;
			   while (tamanhoinverte > 0) 
			   {
				 tamanhoinverte -= 1;
				 stringinvertida += valor.substr(tamanhoinverte,1);
			   }
			   var j = 0;
			   var decimaisformatado = "";
			   var stringformatado = "";
			   for (i = 0; i <= stringinvertida.length; i++) 
			   {
				  j = j + 1;
				  stringformatado = stringformatado + stringinvertida.substr(i,1);
				  if (j == 3 && stringformatado != "") 
				  {
					 if (decimaisformatado == "") { decimaisformatado = stringformatado; } 
					 else { decimaisformatado +=  '.' + stringformatado; }
					 stringformatado = "";
					 j = 0;
				  }
			   }
			   if (j == 2 && tamanho != 4) { decimaisformatado += "." + stringformatado } 
			   else { decimaisformatado += stringformatado; }
		
		tamanhoinverte = decimaisformatado.length;
		var stringfinal = "";
		while (tamanhoinverte > 0) {
			tamanhoinverte = tamanhoinverte - 1;
			stringfinal += decimaisformatado.substr(tamanhoinverte,1);
				
		}
		document.getElementById("val_total").value = stringfinal + "," + decimais;
	}
}

Se alguém puder ajudar, ficarei imensamente agradecido...

 

Abraço a todos

Compartilhar este post


Link para o post
Compartilhar em outros sites

Eis o código

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Procedimentos Realizados</title>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style>
<link href="../css/swih.css" rel="stylesheet" type="text/css">
	<script language="JavaScript">
		<!--
		function valida_campos() 
		{ 
			if (!verifica_data(document.form1.data_procedimento.value))
			{
				alert ("Data incorreta")
				document.form1.data_procedimento.focus()			
				return false 		
			}

		} 

		
		/* ****************************************************************************** */
		function mascara_data_procedimento(data)
		{ document.form1.data_procedimento.value = mascara_data(data); }
		function mascara_hora_inicial(hora)
		{ document.form1.hora_inicial.value = mascara_hora(hora); }
		function mascara_hora_final(hora)
		{ document.form1.hora_final.value = mascara_hora(hora); }
		/* ****************************************************************************** */		
		function calcula()
		{ 
		  var pegaquant = document.getElementById("quantidade").value;
		  var valor	 = document.getElementById("val_unitario").value;
		  
		  valor = valor.replace (".","");
		  valor = valor.replace (",","");
		 // valor = valor * pegaquant;

		  var tamanho		= valor.length;
		  var iniciodecimais = tamanho - 2;
		  alert (tamanho);		
		  if(tamanho > 2) 
			 { 
			   var decimais		= valor.substr(iniciodecimais,3);
			   var inteiros		= valor.substr(0,iniciodecimais);
			   var stringinvertida = "";
			   var tamanhoinverte = tamanho - 2;
			   while (tamanhoinverte > 0) 
			   {
				 tamanhoinverte -= 1;
				 stringinvertida += valor.substr(tamanhoinverte,1);
			   }
			   var j = 0;
			   var decimaisformatado = "";
			   var stringformatado = "";
			   for (i = 0; i <= stringinvertida.length; i++) 
			   {
				  j = j + 1;
				  stringformatado = stringformatado + stringinvertida.substr(i,1);
				  if (j == 3 && stringformatado != "") 
				  {
					 if (decimaisformatado == "") { decimaisformatado = stringformatado; } 
					 else { decimaisformatado +=  '.' + stringformatado; }
					 stringformatado = "";
					 j = 0;
				  }
			   }
			   if (j == 2 && tamanho != 4) { decimaisformatado += "." + stringformatado } 
			   else { decimaisformatado += stringformatado; }
		
		tamanhoinverte = decimaisformatado.length;
		var stringfinal = "";
		while (tamanhoinverte > 0) {
			tamanhoinverte = tamanhoinverte - 1;
			stringfinal += decimaisformatado.substr(tamanhoinverte,1);
				
		}
		document.getElementById("val_total").value = stringfinal + "," + decimais;
	}


		  
//		  novo_valor = pegavalor.replace(".","");
	//	  novo_valor = novo_valor.replace(",",".");
		  
//		  var total   = pegaquant*novo_valor; 
	//	  alert (total);
		//  document.form1.val_total.value = total;
			
		}
		
		/* ****************************************************************************** */	
		function wvalor_mascara_valor(valor)
		{ document.form1.val_unitario.value = mascara_valor(valor); } 		
		
	</script> 
</head>

<body>
<table width="618" border="0" align="center" cellpadding="0" cellspacing="0" class="fat-texto">
  <tr>
	<td><table width="618" border="0" cellpadding="0" cellspacing="0" background="../images/spacer_00557C.gif">
	  <tr>
		<td width="16" height="26"><img src="../images/seta_busca_bco.gif" width="16" height="26"></td>
		<td><font color="#006666" size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><span class="link-preto"> </span><span class="txt-bco-titcons"><font color="#FFFFFF">PROCEDIMENTOS REALIZADOS - NOVO PROCEDIMENTO </font></span></b></font></td>
	  </tr>
	</table>
	  <table width="618" border="0" cellpadding="0" cellspacing="0" background="../images/bg_acao.gif">
		<tr>
		  <td width="122" height="24"><div align="center" class="fat-item-menu-des">novo</div></td>
		  <td width="2"><img src="../images/barra_separa_acao.gif" width="2" height="24"></td>
		  <td width="122" height="24"><div align="center" class="fat-item-menu-des">EDITAR</div></td>
		  <td width="2"><img src="../images/barra_separa_acao.gif" width="2"  height="24"></td>
		  <td width="122" height="24"><div align="center" class="fat-item-menu-des">EXCLUIR</div></td>
		  <td width="2"><img src="../images/barra_separa_acao.gif" width="2" height="24"></td>
		  <td width="122"><div align="center" class="fat-item-menu"><a href="tiss_guiahonorarios_procrealizados.php?guiaconsulta=<? echo $guia_consulta; ?>" class="fat-item-menu">VOLTAR</a></div></td>
		  <td width="2"><img src="../images/barra_separa_acao.gif" width="2" height="24"></td>
		  <td><div align="center" class="fat-item-menu"><a href="java script:window.close()" class="fat-item-menu">FECHAR</a></div></td>
		</tr>
	  </table>
	  <br>
	  <table width="618" border="0" cellspacing="0" cellpadding="0">
		<tr>
		  <td><form name="form1" method="post" action="tiss_guiahonorarios_procrealizados_novo_bd.php">
			<table width="610" border="0" align="center" cellpadding="0" cellspacing="0" class="fat-texto">
			  <tr>
				<td width="130" height="22"> Data</td>
				<td width="100">Hora Inicial </td>
				<td>Hora Final </td>
			  </tr>
			  <tr>
				<td height="22"> <input name="data_procedimento" type="text" class="fat-texto" id="data_procedimento" size="16" maxlength="10" OnKeyUp="mascara_data_procedimento(this.value)"></td>
				<td><input name="hora_inicial" type="text" class="fat-texto" id="hora_inicial" size="10" maxlength="5" OnKeyUp="mascara_hora_inicial(this.value)"></td>
				<td><input name="hora_final" type="text" class="fat-texto" id="hora_final" size="10" maxlength="5" OnKeyUp="mascara_hora_final(this.value)"></td>
			  </tr>
			</table>
					<table width="610" border="0" align="center" cellpadding="0" cellspacing="0" class="fat-texto">
					  <tr>
						<td width="180" height="22"> Via</td>
						<td>Tecnica Utilizada </td>
					  </tr>
					  <tr>
						<td height="22"> <select name="via" class="fat-texto" id="via">
						  <option value="1" selected>Diferentes Vias</option>
						  <option value="2">Mesma Via</option>
						  <option value="3">Única</option>
						</select>
						</td>
						<td><select name="tecnica_utilizada" class="fat-texto" id="tecnica_utilizada">
						  <option value="1" selected>Convencional</option>
						  <option value="2">Videolaparoscopia</option>
						</select>
						</td>
					  </tr>
					</table>
					<table width="610" border="0" align="center" cellpadding="0" cellspacing="0" class="fat-texto">
					  <tr>
						<td height="22"> Código Tabela </td>
					  </tr>
					  <tr>
						<td height="22"> <select name="cod_tabela" class="fat-texto" id="cod_tabela">
						 <?
							$busca = "SELECT * FROM $FAT_TAB_AMB ORDER BY ID ASC";
							include "../bibliotecas/abre_conexao.php";
							$query_busca = ibase_query($busca, $db);
							   while ($show_busca=ibase_fetch_object($query_busca))
							   { echo "<option value=\"$show_busca->ID\">$show_busca->DESCRICAO</option>"; }
							include "../bibliotecas/fecha_conexao.php";
						 ?>
						</select>						</td>
					  </tr>
					</table>
					<table width="610" border="0" align="center" cellpadding="0" cellspacing="0" class="fat-texto">
					  <tr>
						<td width="180" height="22"> Código Procedimento </td>
						<td>Descrição</td>
					  </tr>
					  <tr>
						<td height="22"> <input name="cod_procedimento" type="text" class="fat-texto" id="cod_procedimento" size="20" maxlength="16"></td>
						<td><input name="descricao" type="text" class="fat-texto-up" id="descricao" size="50" maxlength="50"></td>
					  </tr>
					</table>
					<table width="610" border="0" align="center" cellpadding="0" cellspacing="0" class="fat-texto">
					  <tr>
						<td width="130" height="22"> Quantidade</td>
						<td width="130">Valor Unitário </td>
						<td>Valor Total do Procedimento </td>
					  </tr>
					  <tr>
						<td height="22"> <input name="quantidade" type="text" class="fat-texto" id="quantidade" size="10" maxlength="10"></td>
						<td><input name="val_unitario" type="text" class="fat-texto" id="val_unitario" size="14" maxlength="14" onKeyUp="wvalor_mascara_valor(this.value)"></td>
						<td><input name="val_total" type="text" class="fat-texto" id="val_total" size="14" maxlength="14" onFocus="calcula()"></td>
					  </tr>
					</table>
					<br>
		  </form></td>
		</tr>
	  </table>
	</td>
  </tr>
</table>
</body>
</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Está vindo um pouquinho antes do código que postei

/* ****************************************************************************** */
	function mascara_valor(valor)
	{ 
		var wvalor = '';
		var wvalor_limpo = '';
		var wvalor_mascarado = '';
		var waux = '';

	   	wvalor = valor; 	
	
		/* so permite a digitação de números */
		waux = wvalor.substr(wvalor.length-1, 1);
		if((waux != 0)&&
		   (waux != 1)&&
		   (waux != 2)&&
		   (waux != 3)&&
		   (waux != 4)&&
		   (waux != 5)&&
		   (waux != 6)&&
		   (waux != 7)&&
		   (waux != 8)&&
		   (waux != 9))
		{
			wvalor = wvalor.substring(0, wvalor.length-1);
		}	
		
		/* retira a pontuação */
		for (i=0; i <= wvalor.length; i++ )
		{
			if ((wvalor.substr(i,1) != ',') && (wvalor.substr(i,1) != '.'))
			{
				wvalor_limpo = wvalor_limpo + wvalor.substr(i,1);
			} 			
		}
		
		/* retira os zeros */
			var zero = true;
			var i = 0;
			while(zero)
			{
				if (wvalor_limpo.substr(0,1) == '0')
				{
					wvalor_limpo = wvalor_limpo.substr(1, wvalor_limpo.length);
				}
				else
				{
					zero = false;
				}
			}		

		/* insere pontuação */
		  if (wvalor_limpo.length == 0)
		{ 
			wvalor_mascarado =  '0,00';
		   	return wvalor_mascarado; 
		} 

		  if (wvalor_limpo.length == 1)
		{ 
			wvalor_mascarado =  '0,0' + wvalor_limpo;
		   	return wvalor_mascarado; 
		} 

		  if (wvalor_limpo.length == 2)
		{ 
			wvalor_mascarado =  '0,' + wvalor_limpo;
		   	return wvalor_mascarado; 
		} 

		  if (wvalor_limpo.length == 3)
		{ 
			wvalor_mascarado =  wvalor_limpo.substr(0,1) + ',' + wvalor_limpo.substr(1,2);
		   	return wvalor_mascarado; 
		} 

		  if (wvalor_limpo.length == 4)
		{ 
			wvalor_mascarado =  wvalor_limpo.substr(0,2) + ',' + wvalor_limpo.substr(2,2);
		   	return wvalor_mascarado; 
		} 

		  if (wvalor_limpo.length == 5)
		{ 
			wvalor_mascarado =  wvalor_limpo.substr(0,3) + ',' + wvalor_limpo.substr(3,2);
		   	return wvalor_mascarado; 
		} 

		  if (wvalor_limpo.length == 6)
		{ 
			wvalor_mascarado = wvalor_limpo.substr(0,1) + '.' +  wvalor_limpo.substr(1,3) + ',' + wvalor_limpo.substr(4,2);
		   	return wvalor_mascarado; 
		} 

		  if (wvalor_limpo.length == 7)
		{ 
			wvalor_mascarado = wvalor_limpo.substr(0,2) + '.' +  wvalor_limpo.substr(2,3) + ',' + wvalor_limpo.substr(5,2);
		   	return wvalor_mascarado; 
		} 

		  if (wvalor_limpo.length == 8)
		{ 
			wvalor_mascarado = wvalor_limpo.substr(0,3) + '.' +  wvalor_limpo.substr(3,3) + ',' + wvalor_limpo.substr(6,2);
		   	return wvalor_mascarado; 
		} 

		  if (wvalor_limpo.length == 9)
		{ 
			wvalor_mascarado = wvalor_limpo.substr(0,1) + '.' +   wvalor_limpo.substr(1,3) + '.' +  wvalor_limpo.substr(4,3) + ',' + wvalor_limpo.substr(7,2);
		   	return wvalor_mascarado; 
		} 

		  if (wvalor_limpo.length > 9)
		{ 
			alert('Valor maximo 9.999.999,99');
			return '0,00';
		} 
	} 
	
	//-->

-----------------------------------------------------------------------------------

Post Mesclado

-----------------------------------------------------------------------------------

 

O que está faltando neste código?

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.