How do I put mascara on input (Total Value)

Asked

Viewed 368 times

3

inserir a descrição da imagem aqui

Script Membership Value

function mascara(o,f){
  v_obj=o
  v_fun=f
  setTimeout("execmascara()",1)
}
function execmascara(){
  v_obj.value=v_fun(v_obj.value)
}
function mreais(v){
  v=v.replace(/\D/g,"")           //Remove tudo o que não é dígito
  v=v.replace(/(\d{2})$/,",$1")       //Coloca a virgula
  v=v.replace(/(\d+)(\d{3},\d{2})$/g,"$1.$2")   //Coloca o primeiro ponto
  return v
}

Input of Accession Value:

<input type="text" id="txt1" name="adesao" class="form-control calcular" placeholder="R$" onkeypress="mascara(this,mreais)" onkeyup="calcular()">

Script Total Value

function calcular() {
    var soma = $('.calcular').get().reduce(function(soma, el) {
        return (parseFloat(el.value.replace(/\./g, "").replace(",", "."), 10) || 0) + soma;
    }, 0);
    document.getElementById('result').value = soma;


}

Input Total Value

<input type="text" class="form-control" name="total" id="result" readonly>

1 answer

3


Apply the mask function after inputar the result in it.

document.getElementById('result').value = soma;

// Aplica Máscara
mascara(document.getElementById('result'), mreais);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.