I have a Form where I multiply two variables under the following conditions:
Only multiplied by% with% by% by%, if% by% is filled, otherwise total field by% by% by%.
The error happens when I try to submit the form and the variavel1
is not filled, error occurs
Failed to convert property of type java.lang.String to required type java.math.BigDecimal for property totalCurrent; nested exception is java.lang.NumberFormatException
When the variavel2
is filled, the form is saved, but when it is not filled the error occurs.
I've already thought about putting variavel2
to value = 1, but as I'm new to JavaScript I have no idea how to return varialvel1
plus value 1 to variavel2
.
HTML:
<html>
<body>
<form>
<input id="valor1" onblur="calcular()"><br>
<input id="valor2" onblur="calcular()"><br>
<input id="total"><br>
<button type="submit">
</form>
</body>
</html>
JavaScript code
function calcular(){
var valor1 = (document.getElementById('valor1').value);
var valor2 = (document.getElementById('valor2').value);
var total;
if (valor2==null || valor2==""){
total = valor1;
return document.getElementById('total').value = total;
}else {
valor1 = parseFloat(valor1.replace(',','.'));
valor2 = parseFloat(valor2.replace(',','.'));
total = valor1 * valor2;
return document.getElementById('total').value = total;
}
}