Mask in input field

Asked

Viewed 6,514 times

0

Hello

I have an input where I enter a currency value of R$1,000.00, I want the field to have a mask to help the user fill in, but I want javascript not to take the value with the mask, but without the mask, be displayed on the console the value like this: 100000.

Thank you

2 answers

2


I advise the use of the library https://igorescobar.github.io/jQuery-Mask-Plugin/

example below your use

$(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>
<script src="https://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<input class="money" id="input" type="text">


<span id="value"> </span>

  • 1

    Perfect, Thank you

2

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="">

Browser other questions tagged

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