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

mascara finaceira em javascript que aceite valor negativo


JimmyPage

Pergunta

bom dia pessoal, estou usando a seguinte função:

// -- Formata o numero na entrada especificamente para o campo Valor Total do Item, que passa a aceitar
// -- valores negativos.
// -- Utilização:
// -- onKeyPress="return(currencyFormat(this,",",".",event))
function currencyFormatValorItem(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
if (whichCode == 0) return true; // Delete
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;}

acontece que estou com o seguinte problema, ele aceita que eu digito o símbolo "-", mas ele assume que este simbolo faz parte dos valores, e o formato da mascara pode sair assim: -,11 ; -.111,11 e ---.---.---,--

preciso de ajuda, pois com valores positivos dá certo, mas negativo não e posso digitar o simbolo "-" qnts vezes quiser,

no aguardo de ajuda !!!

se algume souber de uma outra função, por favor me passe

Editado por fercosmig
Adicionar tag CODE
Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

  • 0

Aff.... pra fazer isso precisa de um códiguzão!!!!! kkkkkkkkk

Eu uso isto:

<script type="text/javascript">
function mascara_num(obj){
  valida_num(obj)
  if (obj.value.match("-")){
    mod = "-";
  }else{
    mod = "";
  }
  valor = obj.value.replace("-","");
  valor = valor.replace(",","");
  if (valor.length >= 3){
    valor = poe_ponto_num(valor.substring(0,valor.length-2))+","+valor.substring(valor.length-2, valor.length);
  }
  obj.value = mod+valor;
}
function poe_ponto_num(valor){
  valor = valor.replace(/\./g,"");
  if (valor.length > 3){
    valores = "";
    while (valor.length > 3){
      valores = "."+valor.substring(valor.length-3,valor.length)+""+valores;
      valor = valor.substring(0,valor.length-3);
    }
    return valor+""+valores;
  }else{
    return valor;
  }
}
function valida_num(obj){
  numeros = new RegExp("[0-9]");
  while (!obj.value.charAt(obj.value.length-1).match(numeros)){
    if(obj.value.length == 1 && obj.value == "-"){
      return true;
    }
    if(obj.value.length >= 1){
      obj.value = obj.value.substring(0,obj.value.length-1)
    }else{
      return false;
    }
  }
}
</script>
<input type="text" onkeyup="mascara_num(this);" />

Acabei tendo um tempinho, então taí o código completo!!!!

Editado por Gigabyte
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,1k
    • Posts
      651,8k
×
×
  • Criar Novo...