Field mask input [duplicate]

0

Hello

I have an input where I enter a value in currency $ 1,000.00, I want the field to have a mask to help the user in the fill, but I want the javascript does not take the value with the mascara, but without the mask is displayed on the console as: 100000.

Thank you

    
asked by anonymous 18.11.2016 / 20:05

2 answers

2

I recommend using the link library

example below your usage

$(document).ready(function(){
   $('.money').mask('000.000.000.000.000,00', {reverse: true});
  
  $(".money").change(function(){
    $("#value").html($(this).val().replace(/\D/g,''))
  })
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<input class="money" id="input" type="text">


<span id="value"> </span>
    
18.11.2016 / 20:16
0

Simple example using JavaScript

String.prototype.reverse = function(){
  return this.split('').reverse().join(''); 
};

function mascaraMoeda(campo,evento){
  var tecla = (!evento) ? window.event.keyCode : evento.which;
  var valor  =  campo.value.replace(/[^\d]+/gi,'').reverse();
  var resultado  = "";
  var mascara = "##.###.###,##".reverse();
  for (var x=0, y=0; x<mascara.length && y<valor.length;) {
    if (mascara.charAt(x) != '#') {
      resultado += mascara.charAt(x);
      x++;
    } else {
      resultado += valor.charAt(y);
      y++;
      x++;
    }
  }
  campo.value = resultado.reverse();
}
<p>Exemplo de mascara para Moeda em JavaScript</p>
<input type="Text" size="12" onKeyUp="mascaraMoeda(this, event)"  value="">
    
19.11.2016 / 02:16