Ir para conteúdo

POWERED BY:

Arquivado

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

riopardo

Formatar campo em JavaScript

Recommended Posts

No código abaixo estou com o seguinte problema: preciso formatar :<input readonly name="totalvalor" type="text" value="0" size="20" > para que ele receba os dados que é valor em moeda que é dinamico sendo assim: var precovalor = Number(document.form1.preco.value); sendo "valor" pegando a string dinamica em <input name="preco" type="hidden" id="preco" value="campo dinamico - valor pego no BD"> - mas o problema é que ele vem em valor moeda Se alguem poder me ajudar Agradeço desde já<HTML><HEAD><TITLE> teste </TITLE><script language="JavaScript">function calcula(){var treistreis = Number(document.form1.treistreis.value);var treisseis = Number(document.form1.treisseis.value);var treisnove = Number(document.form1.treisnove.value);var quatrodois = Number(document.form1.quatrodois.value);var precovalor = Number(document.form1.preco.value);var total = ((treistreis) + (treisseis) + (treisnove) + (quatrodois));var totalvalor = ( (total) * (precovalor));document.form1.total.value = total;document.form1.totalvalor.value = totalvalor;}</script></HEAD><BODY BGCOLOR="#FFFFFF"> <form ACTION="" METHOD="POST" name="form1"> <table width="550" border="0" cellspacing="3" cellpadding="3"> <tr> <td> </td> </tr> <tr> <td>Soma: <input readonly type="text" name="total" size="8" value="0">Valor:<label><input readonly name="totalvalor" type="text" value="0" size="20" ></label><br> <table width="400" border="1" cellspacing="3" cellpadding="3"> <tr> <td width="100">33 - <label> <input name="treistreis" type="text" onBlur="calcula();" id="treistreis" size="3"> </label></td> <td width="100">36 - <label> <input name="treisseis" type="text" onBlur="calcula();" id="treisseis" size="3"> </label></td> <td width="100">39 - <label> <input name="treisnove" type="text" onBlur="calcula();" id="treisnove" size="3"> </label></td> <td width="100">42 - <label> <input name="quatrodois" type="text" onBlur="calcula();" id="quatrodois" size="3"> </label></td> </tr> </table><label> <input type="submit" name="Submit" value="Gravar"> <input name="pedidoID" type="hidden" id="pedidoID" value="campo dinamico"> <input name="preco" type="hidden" id="preco" value="campo dinamico"> <input name="customerID" type="hidden" id="customerID" value="campo dinamico"> <input name="idproduto" type="hidden" id="idproduto" value="campo dinamico"></label></td> </tr> </table></form></BODY></HTML>

Compartilhar este post


Link para o post
Compartilhar em outros sites

]<html> <head> <title>Máscaras para Formulários</title> [color=red]<script language="JavaScript"> [/color][color=gray]/*** * Descrição.: formata um campo do formulário de * acordo com a máscara informada... * Parâmetros: - objForm (o Objeto Form) * - strField (string contendo o nome * do textbox) * - sMask (mascara que define o * formato que o dado será apresentado, * usando o algarismo "9" para * definir números e o símbolo "!" para * qualquer caracter... * - evtKeyPress (evento) * Uso.......: <input type="textbox" * name="xxx"..... * onkeypress="return txtBoxFormat(document.rcfDownload, 'str_cep', '99999-999', event);"> * Observação: As máscaras podem ser representadas como os exemplos abaixo: * CEP -> 99.999-999 * CPF -> 999.999.999-99 * CNPJ -> 99.999.999/9999-99 * Data -> 99/99/9999 * Tel Resid -> (99) 999-9999 * Tel Cel -> (99) 9999-9999 * Processo -> 99.999999999/999-99 * C/C -> 999999-! * E por aí vai... ***/[/color][color=red]function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {      var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;      if(document.all) { // Internet Explorer        nTecla = evtKeyPress.keyCode; }      else if(document.layers) { // Nestcape        nTecla = evtKeyPress.which;      }      sValue = objForm[strField].value;      // Limpa todos os caracteres de formatação que      // já estiverem no campo.      sValue = sValue.toString().replace( "-", "" );      sValue = sValue.toString().replace( "-", "" );      sValue = sValue.toString().replace( ".", "" );      sValue = sValue.toString().replace( ".", "" );      sValue = sValue.toString().replace( "/", "" );      sValue = sValue.toString().replace( "/", "" );      sValue = sValue.toString().replace( "(", "" );      sValue = sValue.toString().replace( "(", "" );      sValue = sValue.toString().replace( ")", "" );      sValue = sValue.toString().replace( ")", "" );      sValue = sValue.toString().replace( " ", "" );      sValue = sValue.toString().replace( " ", "" );      fldLen = sValue.length;      mskLen = sMask.length;      i = 0;      nCount = 0;      sCod = "";      mskLen = fldLen;      while (i <= mskLen) {        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))        if (bolMask) {          sCod += sMask.charAt(i);          mskLen++; }        else {          sCod += sValue.charAt(nCount);          nCount++;        }        i++;      }      objForm[strField].value = sCod;      if (nTecla != 8) { // backspace        if (sMask.charAt(i-1) == "9") { // apenas números...          return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9        else { // qualquer caracter...          return true;        } }      else {        return true;      }    }[/color][color=gray]//Fim da Função Máscaras Gerais[/color]</head> <body> <font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Formatação de várias Máscaras de Entradas para campos de Formulários</strong></font> <form name="Form"> <table border="0" cellspacing="2" cellpadding="2"> <tr> <td width="66"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">cep  </font></td> <td width="128"> <input type="text" name="str_cep" maxlength="10" size="10" [b]onkeypress="return txtBoxFormat(document.Form, 'str_cep', '99.999-999', event);"[/b]> </td> </tr> <tr> <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">cpf  </font></td> <td> <input type="text" name="str_cpf" maxlength="14" size="14" [b]onkeypress="return txtBoxFormat(document.Form, 'str_cpf', '999.999.999-99', event);"[/b]> </td> </tr> <tr> <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">cnpj</font></td> <td><input type="text" name="str_cnpj" maxlength="18" size="18" [b]onkeypress="return txtBoxFormat(document.Form, 'str_cnpj', '99.999.999/9999-99', event);"[/b]></td> </tr> <tr> <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">data</font></td> <td><input type="text" name="str_data" maxlength="10" size="10" [b]onkeypress="return txtBoxFormat(document.Form, 'str_data', '99/99/9999', event);"[/b]></td> </tr> <tr> <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">tel resid</font></td> <td><input type="text" name="str_tel2" maxlength="13" size="13" [b]onkeypress="return txtBoxFormat(document.Form, 'str_tel2', '(99) 999-9999', event);"[/b]></td> </tr> <tr> <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">tel cel </font></td> <td> <input type="text" name="str_tel" maxlength="14" size="14" [b]onkeypress="return txtBoxFormat(document.Form, 'str_tel', '(99) 9999-9999', event);"[/b]> </td> </tr> <tr> <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Processo</font></td> <td><input type="text" name="str_proc" maxlength="20" size="20" [b]onkeypress="return txtBoxFormat(document.Form, 'str_proc', '99.999999999/9999-99', event);"[/b]></td> </tr> <tr> <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">conta  </font></td> <td> <input type="text" name="str_cc" maxlength="6" size="10" [b]onkeypress="return txtBoxFormat(document.Form, 'str_cc', '9999-!', event);"[/b]> </td> </tr> </table> </form> </body> </html>

Alica CWB

Compartilhar este post


Link para o post
Compartilhar em outros sites

alicacwb,O campo que preciso é valor monetário na mascara tem como configurar para R$ e tambem o imput é readonly ele le e escreve o resultado das variáveis mensionadas

Compartilhar este post


Link para o post
Compartilhar em outros sites

Resolvi.... Código que eu precisava :---><script language="JavaScript">var trocaV = /,/g;var trocaP = /\./g;function calcula(){var treistreis = (document.form1.treistreis.value).replace(trocaV,".");var treisseis = (document.form1.treisseis.value).replace(trocaV,".");var treisnove = (document.form1.treisnove.value).replace(trocaV,".");var quatrodois = (document.form1.quatrodois.value).replace(trocaV,".");var precovalor = (document.form1.preco.value).replace(trocaV,".");var total = ((treistreis-0) + (treisseis-0) + (treisnove-0) + (quatrodois-0));var totalvalor = ( (total) * (precovalor/100));//total=Math.round(total*100)/100; //if(total.toString().indexOf(".")==-1) {total+=".00";}//total=total.toString().replace(trocaP,",");//parte=total.split(",");//(parte[1].length == 1)? total+="0":total;document.form1.total.value = total;totalvalor=Math.round(totalvalor*100)/100; if(totalvalor.toString().indexOf(".")==-1) {totalvalor+=".00";}totalvalor=totalvalor.toString().replace(trocaP,",");parte=totalvalor.split(",");(parte[1].length == 1)? totalvalor+="0":totalvalor;document.form1.totalvalor.value = (totalvalor);}</script>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Fabio crie um topico novo visto que este é de 4 anos atras e muito mudou dela para ca

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.