Compare real values and inject value into input with Jquery

Asked

Viewed 103 times

-1

Guys, I have a form where I have a field to type in values. This field has a mask in jquery that formats the values for Brazilian real 0.00, mvalor function, which is below, but this field can only accept values in real greater than 25.00, or it is necessary to validate this field so that the user can only enter values greater than 25.00. A friend passes me this function below, it works well, if someone type a value less than 25,00 shows an error message warning and injects the value 25,00 in the input field, in this format in real, but if I type a value above 999,00, for example 1,000,00 an error happens because it understands that the entered value is less than 25.00, I realized that this occurs because the function checks only the numbers before the point, if before the point is greater than 25 regardless of whether it is real or numerical blz, but if someone type 1,000,00 which is less than 25,00, where the number before the point is only 1 so it does not do what is proposed and gives a mistake. If I type in the field 25.000,00 of the right, with that I realize that validates only the number that comes before the point, what comes after the function ignores.

I don’t have enough knowledge in jquery to fix this problem, if anyone can help me, I will be very grateful.

<script>


    $('input[name=amount]').focusout(function() {
    var valorDigitado = parseFloat($(this).val());
    if (valorDigitado < 25.00) {
       $('input[name="amount"]').val("25,00");
       showError_amount();
       } else {
        $("#error_amount").hide();
       }

  });

  function mvalor(i) {
  var v = i.value.replace(/\D/g,'');
  v = (v/100).toFixed(2) + '';
  v = v.replace(".", ",");
  v = v.replace(/(\d)(\d{3})(\d{3}),/g, "$1.$2.$3,");
  v = v.replace(/(\d)(\d{3}),/g, "$1.$2,");
  i.value = v;
  }

</script>

1 answer

0


1.000,00 is considered to be less than 25.00 because in Javascript the decimals are separated by a point, that is, 1.000,00 is the same thing as 1.00 or just 1, which is less than 25.

You can solve by removing the point of a thousand from the entered value and replacing the comma by a dot. By typing 1.000,00, will stay 1000.00 (mil), which is greater than 25 (or 25.00).

Just make two re-places in value inside the parseFloat:

parseFloat($(this).val().replace(/\./g,"").replace(",","."))

Here .replace(/\./g,"") replaces all possible thousand separator points that the mask inserts.

Behold:

$('input[name=amount]').focusout(function() {
 var valorDigitado = parseFloat($(this).val().replace(/\./g,"").replace(",","."));
 if (valorDigitado < 25.00) {
    $('input[name="amount"]').val("25,00");
    showError_amount();
    } else {
     $("#error_amount").hide();
    }

});

function mvalor(i) {
var v = i.value.replace(/\D/g,'');
v = (v/100).toFixed(2) + '';
v = v.replace(".", ",");
v = v.replace(/(\d)(\d{3})(\d{3}),/g, "$1.$2.$3,");
v = v.replace(/(\d)(\d{3}),/g, "$1.$2,");
i.value = v;
}

function showError_amount(){
  $("#error_amount").show().text("Menor que 25");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<input type="text" name="amount" oninput="mvalor(this)">
<div id="error_amount"></div>

  • 1

    Thanks for the help, it solved my problem.

Browser other questions tagged

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