Jump to content
Fórum Script Brasil
  • 0

[resolvido] Formatar Moeda


misterviralata

Question

ola amigos!

Procurei aqui no forum algum script para formatar moeda, encontrei varios, mas a maioria só funciona no IE. Encontrei um que funciona no FF e no IE, mas o campo aceita letras e numeros, não manjo muito de javascript, se alguém puder me ajudar a melhorar o script para aceitar somente numeros, fico Grato!

segue abaixo o script

<html>
<head>
<title>Noticías on-line</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>

<script>
function FormataValor(id,tammax,teclapres) {
    
        if(window.event) { // Internet Explorer
         var tecla = teclapres.keyCode; }
        else if(teclapres.which) { // Nestcape / firefox
         var tecla = teclapres.which;
        }
    

vr = document.getElementById(id).value;
vr = vr.toString().replace( "/", "" );
vr = vr.toString().replace( "/", "" );
vr = vr.toString().replace( ",", "" );
vr = vr.toString().replace( ".", "" );
vr = vr.toString().replace( ".", "" );
vr = vr.toString().replace( ".", "" );
vr = vr.toString().replace( ".", "" );
tam = vr.length;

if (tam < tammax && tecla != 8){ tam = vr.length + 1; }

if (tecla == 8 ){ tam = tam - 1; }

if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
if ( tam <= 2 ){
document.getElementById(id).value = vr; }
if ( (tam > 2) && (tam <= 5) ){
document.getElementById(id).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
if ( (tam >= 6) && (tam <= 8) ){
document.getElementById(id).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
if ( (tam >= 9) && (tam <= 11) ){
document.getElementById(id).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.getElementById(id).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.getElementById(id).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 action="" method="POST">
<input name="valor" type="text" id="valor" onKeyPress="FormataValor(this.id, 10, event)" size="10" maxlength="10"/>
<input type="submit" >
</form>
</body>
</html>

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

eu utilizo a funcao abaixo:

<script>

<!--

function currencyFormat(fld, milSep, decSep, 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; // Enter

key = String.fromCharCode(whichCode); // Get key value from key code

if (strCheck.indexOf(key) == -1) return false; // Not a valid key

len = fld.value.length;

for(i = 0; i < len; i++)

if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;

aux = '';

for(; i < len; i++)

if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);

aux += key;

len = aux.length;

if (len == 0) fld.value = '';

if (len == 1) fld.value = '0'+ decSep + '0' + aux;

if (len == 2) fld.value = '0'+ decSep + aux;

if (len > 2) {

aux2 = '';

for (j = 0, i = len - 3; i >= 0; i--) {

if (j == 3) {

aux2 += milSep;

j = 0;

}

aux2 += aux.charAt(i);

j++;

}

fld.value = '';

len2 = aux2.length;

for (i = len2 - 1; i >= 0; i--)

fld.value += aux2.charAt(i);

fld.value += decSep + aux.substr(len - 2, len);

}

return false;

}

use tambem a funcao delete para apagar o campo:

function tecla(event){

if(event == 46){

form1.Valor.value = "";

}

if(event == 8){

var tamanho = document.getElementById("Valor").value.length;

//form1.document.getElementById["Valor"].value = form1.document.getElementById["Valor"].value.substring(0,tamanho);

form1.Valor.value = form1.Valor.value.substring(0,tamanho);

}

}

Link to comment
Share on other sites

  • 0

opa, desculpa a falha ai, na caixa de texto onde voce digitará os numeros coloque o seguinte:

onKeyPress="return(currencyFormat(this,',','.',event))"
a funcao tecla, para deletar o conteudo da caixa sem precisar selecionar nada, apenas pressionando a tecla delete, voce deve colocar no body:

<body onKeyUp="tecla(event.keyCode)" >

fiz esta funcao, porque se voce utilizar backspace não funciona legal...

Edited by Halfar
Link to comment
Share on other sites

  • 0

Tenta ae:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>

<script>
function BloqueiaLetras(evento)
{
var tecla;
var campo = document.getElementById("valor").value;
    if(window.event) { // Internet Explorer
        tecla = event.keyCode;
    }
    else { // Firefox
        tecla = evento.which;
    }    
    
  if(tecla >= 48 && tecla <= 57 || tecla == 8) 
  {
      FormataValor(campo, 10, tecla);
  }
  else
      return false;
}

function FormataValor(valor,tammax,tecla) {
        if (tecla >= 48 && tecla <= 57)
        {
            vr = valor;
            vr = vr.toString().replace( "/", "" );
            vr = vr.toString().replace( "/", "" );
            vr = vr.toString().replace( ",", "" );
            vr = vr.toString().replace( ".", "" );
            vr = vr.toString().replace( ".", "" );
            vr = vr.toString().replace( ".", "" );
            vr = vr.toString().replace( ".", "" );
            tam = vr.length;
            
            if (tam < tammax && tecla != 8){ tam = vr.length + 1; }
            
            if (tecla == 8 ){ tam = tam - 1; }
            
            if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
            {
                if ( tam <= 2 )
                {
                    document.getElementById("valor").value = vr; 
                }
                if ( (tam > 2) && (tam <= 5) )
                {
                    document.getElementById("valor").value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); 
                }
                if ( (tam >= 6) && (tam <= 8) )
                {
                    document.getElementById("valor").value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
                }
                if ( (tam >= 9) && (tam <= 11) )
                {
                    document.getElementById("valor").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.getElementById("valor").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.getElementById("valor").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 action="" method="POST">
<input name="valor" type="text" value="" id="valor" onKeyPress="return BloqueiaLetras(event)" size="10" maxlength="10"/>
<input type="submit">
</form>
</body>
</html>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152k
    • Total Posts
      651.6k
×
×
  • Create New...