Good afternoon, I have the function below javascript to hold the account when you leave 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 goes in as if the discount was greater than the value, and it informs the alert. Now if the value is 15.00 and I place the discount of 1.00, it works, and shows the total amount to pay right, of 14.00. By what oq to understand, he is doing the count of the first number of 15, q is 1, and picks up 5. I do not know why this is occurring.