Ir para conteúdo
Fórum Script Brasil
  • 0

Mascaras De Entrada


Guest paulohirth

Pergunta

Guest paulohirth

to com um probleminha....

to presisando de um codigo de mascara de entrada para valores decimai.

eu tenho um codigo q estou enviando junto, mas o problema é que quando digito 12 por exemplo, fica 0.12.

se alguém tiver algum codigo onde digito 12 e fica 12,00,

desde já agradeco...

paulo hirth

Codigo

<script LANGUAGE="JavaScript">

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;

}

</script>

<form>Defina o Valor em Reais : <input type=text name=test length=15 onKeyPress="return(currencyFormat(this,',','.',event))">

</form>

Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

  • 0

Ve se este codigo Serve:

<html>

<head>

<title>Formatação de Valores monetarios</title>

</head>

<body>

<script>

function Limpar(valor, validos) {

// retira caracteres invalidos da string

var result = "";

var aux;

for (var i=0; i < valor.length; i++) {

aux = validos.indexOf(valor.substring(i, i+1));

if (aux>=0) {

result += aux;

}

}

return result;

}

//Formata número tipo moeda usando o evento onKeyDown

function Formata(campo,tammax,teclapres,decimal) {

var tecla = teclapres.keyCode;

vr = Limpar(campo.value,"0123456789");

tam = vr.length;

dec=decimal

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 <= dec )

{ campo.value = vr ; }

if ( (tam > dec) && (tam <= 5) ){

campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }

if ( (tam >= 6) && (tam <= 8) ){

campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, 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 - dec, 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 - dec, tam ) ; }

if ( (tam >= 15) && (tam <= 17) ){

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>

<input type="text" name="T1" size="20" onKeydown="Formata(this,20,event,2)"></p>

</p>

</body>

</html>

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      652k
×
×
  • Criar Novo...