Mascara Javascript

Asked

Viewed 1,235 times

0

Good afternoon guys, I need a little help from you.

I need a mask for a number field on my system, where the user can type from 0.0 to 130.00 if the value is greater than 130 will only accept 999.99.

Could someone help me?

  • Question: Can you use jQuery in your project? Because then it could be applied jQuery.Mask which is a very useful jQuery feature for your problem. In case I can’t see a native solution.

  • You want it valid or limit to 3 digits before the comma?

1 answer

2

Just apply maxlength counting all characters in 999,99, which in case would be 6, basically would give to apply the maxlength for almost any mask you find.

Of course some jQuery plugins and the like already have configuration for this, but are exceptions.

Examples of money/money masks on the site:

With jQuery:

$("#meuDinheiro").maskMoney();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://plentz.github.io/jquery-maskmoney/javascripts/jquery.maskMoney.min.js"></script>

<input type="text" id="meuDinheiro" data-thousands="." data-decimal="," data-prefix="" maxlength="6" />

Now if you wish me to turn 999,99 can limit to 7 and apply keyup

$("#meuDinheiro").maskMoney().on("keyup", function () {
   if (this.value.length > 6) {
       this.value = "999,99";
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://plentz.github.io/jquery-maskmoney/javascripts/jquery.maskMoney.min.js"></script>

<input type="text" id="meuDinheiro" data-thousands="." data-decimal="," data-prefix="" maxlength="7" />


No jQuery (Javascript and Regex)

Of response: /a/241646/3635

function formatarMoeda() {
  var elemento = document.getElementById('valor');
  var valor = elemento.value;
  
  valor = valor + '';
  valor = parseInt(valor.replace(/[\D]+/g,''));

  if (!isNaN(valor)) {
    valor = valor + '';
    valor = valor.replace(/([0-9]{2})$/g, ",$1");

    if (valor.length > 6) {
        valor = valor.replace(/([0-9]{3}),([0-9]{2}$)/g, ".$1,$2");
    }
  } else {
     valor = '';
  }

  elemento.value = valor;
}
<input type="text" id="valor" onkeyup="formatarMoeda();" maxlength="6" />

Now if you wish me to turn 999,99 can limit to 7 and apply keyup

function formatarMoeda() {
  var elemento = document.getElementById('valor');
  var valor = elemento.value;
  
  valor = valor + '';
  valor = parseInt(valor.replace(/[\D]+/g,''));

  if (!isNaN(valor)) {
    valor = valor + '';
    valor = valor.replace(/([0-9]{2})$/g, ",$1");

    if (valor.length > 6) {
        valor = '999,99'; //Mudei esta linha
    }
  } else {
     valor = '';
  }

  elemento.value = valor;
}
<input type="text" id="valor" onkeyup="formatarMoeda();" maxlength="7" />

  • From what I understand, in fact, can any value between 0.00 and 130.00 and if it exceeds 130.00 all values will be invalid until it reaches the value 999.99

  • @Pauloroberto now I was in doubt too.

  • Tbm understood as @Paulo said.

  • @Pauloroberto is right, if the value goes beyond 130,00 the field has to stay 999,99

  • @Leonardomacedo I do not find it very practical for the user to keep modifying the value, but I edited the answer and added two examples

  • @Guillhermenascimentop. I apologize face my doubt is totally different, as I’m messing with a system that is webforms, I’m having a certain difficulty in understanding the radios, but I will open another topic explaining better what I really need help

  • @Guilhermenascimentop. https://answall.com/questions/263951/exce%C3%A7%C3%A3o-para-um-campo-webforms está would really be my question

Show 2 more comments

Browser other questions tagged

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