Javascript function not working correctly

Asked

Viewed 56 times

1

Good afternoon, I have the function below javascript to carry out the account when leaving the discount field:

function calcular1() {
  String.prototype.formatMoney = function() {
    var v = this;

    if (v.indexOf('.') === -1) {
      v = v.replace(/([\d]+)/, "$1,00");
    }

    v = v.replace(/([\d]+)\.([\d]{1})$/, "$1,$20");
    v = v.replace(/([\d]+)\.([\d]{2})$/, "$1,$2");
    v = v.replace(/([\d]+)([\d]{3}),([\d]{2})$/, "$1.$2,$3");

    return v;
  };
  var v1 = document.getElementById("txtValoraPagar").value;
  var v2 = document.getElementById("txtDesconto").value;
  var txtTotalPagar = document.getElementById("txtTotalPagar");
  if ((v2.replace("R$", "").replace(",", ".").replace(/\s*/g, '')) > (v1.replace(",", ".").replace(/\s*/g, ''))) {
    alert('Valor do desconto não pode ser maior do que o valor a receber.');
    document.getElementById("txtDesconto").value = '';
    return false;
  }
  if ((v1 != "") && (v2 != "") && (v1.replace(",", ".").replace(/\s*/g, '') >= v2.replace("R$", "").replace(",", ".").replace(/\s*/g, ''))) {
    txtTotalPagar.value = (eval(v1.replace(",", ".") - eval(v2.replace("R$", "").replace(",", "."))));
    txtTotalPagar.value = 'R$ ' + String(txtTotalPagar.value).formatMoney();
  }

For example, if the value is 15.00 and the discount is 5.00 it enters as if the discount was greater than the value, and informs Alert. Now if the value is 15.00 and I put the discount of 1.00, it works, and shows the total amount to pay right, of 14.00. By what to understand, he is counting the first number of the 15, q is 1, and takes the 5. I do not know why this is happening.

  • The result as you are describing is correct because you are comparing instances of characters and not numbers.

1 answer

1


You have to compare numbers instead of characters.

Just another thing, name your most intuitive variables. You can’t have a variable named v1, v2. What do they mean? It becomes clearer variables with name 'value' and 'discount' which are what they really represent.

function calcular2() {
   var txtValor = document.getElementById("txtValoraPagar").value;
   var txtDesconto = document.getElementById("txtDesconto").value;
   var txtTotalPagar = document.getElementById("txtTotalPagar");
   var valor =0;
   var desconto =0; 
   if(txtValor != null)
     valor = Number(txtValor.replace(/[R\$ ]/g, '').replace(',', '.'));
   if(txtDesconto != null)
     desconto = Number(txtDesconto.replace(/[R\$ ]/g, '').replace(',', '.'));
   if(desconto > valor){
     alert('Valor do desconto não pode ser maior do que o valor a receber.');
     return false;
   }else if (valor >= desconto){
   	 txtTotalPagar.value = 'R$ ' + (valor - desconto);
   } 
}
Valor: <input type="text" id="txtValoraPagar" value="R$ ">
<br>
Desconto: <input type="text" id="txtDesconto" value="R$ ">
<br>
Total: <input type="text" id="txtTotalPagar" readonly="true" placeholder="Valor final">
<br>
<button onclick="calcular2()">Calcular</button>

  • It works, but when the value passes one thousand, it appears the point, for example: 5,000.00, it does not do any action.

  • This 'dot' is normal for the numerical display pattern. It always appears 3 in 3 houses. For example: 1,000 or 1,000,000

  • Yes, but when the point appears, it does not enter the function, nothing happens.

  • Instead of /[R$ ]/g as REGEX, use /[R$ . ]/g that will work

  • It worked, thank you very much.

  • Another thing you can do is instead use <input type="text"> to use the <input type="number">. So you will no longer worry about regex, as the field will already return a number when it is requested. I did the example with <input type="text"> to be the same as what you were already doing and also because I didn’t know if this would suit your purpose.

Show 1 more comment

Browser other questions tagged

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