How do I put mask in the input (Total Value)

3

ScriptAdhesionValue

functionmascara(o,f){v_obj=ov_fun=fsetTimeout("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
}

Adhesion Value Input:

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

Total Value Script

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;


}

Total Value Input

<input type="text" class="form-control" name="total" id="result" readonly>
    
asked by anonymous 21.12.2016 / 16:57

1 answer

3

Applies the mask function after inputting the result into it.

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

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