l need help to finish my php project. l will like to have a text fields V1,V2,V3 and result/output to accept only decimal numbers (4 decimal place ie 0.0000) or automatically turns numbers to four decimal pace and have a read-only field generate the total sum of the 3 text fields without reloading the form.
eg text field v1+text field v2+text field v3=text field result
the text field should accept on four decimal numbers or automatically turn numbers to four decimal place.
I am using the following sum function;
function sum()
{
var TotalLBCReceipts = num("TotalLBCReceipts");
var TotalKaaseReceipts = num("TotalKaaseReceipts");
var TakoradiToFactory = num("TakoradiToFactory2");
var KaaseToFactory = num("KaaseToFactory2");
var SampleResidue = num("SampleResidue");
var Sweepings = num("Sweepings");
var OrganicCocoa = num("OrganicCocoa2");
var Confiscated = num("Confiscated");
var Cuttings = num("Cuttings");
var r = document.getElementById("GRANDTOTAL1");
if (r != null)
{
r.value = TotalLBCReceipts +
TotalKaaseReceipts +
TakoradiToFactory +
KaaseToFactory +
SampleResidue +
Sweepings +
OrganicCocoa +
Confiscated+Cuttings;
}
}
And grabbing the Values using;
function num(id)
{
var e = document.getElementById(id);
if (e != null)
{
var v = e.value;
if (/^\d+$/.test(v))
{
return parseInt(v, 10);
}
}
return 0;
}
thanks.
Google is your friend... As is StackOverflow!!
You need to use JavaScript here, perhaps as well as PHP...
Restrict to Numbers with decimal place:
Restricting input to textbox: allowing only numbers and decimal point
<HTML>
<HEAD>
<SCRIPT language=Javascript>
<!--
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<INPUT id="txtChar" onkeypress="return isNumberKey(event)"
type="text" name="txtChar">
</BODY>
</HTML>
Sum Numbers with readonly field:
adding the text box values and display it using javascript
<form name="myFormName">
<p><input type="text" name="myInputName1" value="25.3"></p>
<p><input type="text" name="myInputName2" value="14.2"></p>
</form>
<div id="total"></div>
<script type="text/javascript>
var total = parseFloat(0, 10);
total += parseFloat(document.myFormName.myInputName1.value, 10);
total += parseFloat(document.myFormName.myInputName2.value, 10);
document.getElementById("total").innerHTML = "Total is " + total;
</script>
Round to 4 Decimal Places;:
https://stackoverflow.com/a/7269919/1305169
Math.round(val*10000)/10000
Hope this helps!
EDIT:
You need to change your Code to the following;
Your sum
Function:
function sum()
{
var TotalLBCReceipts = num("TotalLBCReceipts");
var TotalKaaseReceipts = num("TotalKaaseReceipts");
var TakoradiToFactory = num("TakoradiToFactory2");
var KaaseToFactory = num("KaaseToFactory2");
var SampleResidue = num("SampleResidue");
var Sweepings = num("Sweepings");
var OrganicCocoa = num("OrganicCocoa2");
var Confiscated = num("Confiscated");
var Cuttings = num("Cuttings");
var r = document.getElementById("GRANDTOTAL1");
if (r != null)
{
r.value = Math.round((
TotalLBCReceipts +
TotalKaaseReceipts +
TakoradiToFactory +
KaaseToFactory +
SampleResidue +
Sweepings +
OrganicCocoa +
Confiscated +
Cuttings) * 10000) / 10000;
}
}
And your num
function:
function num(id)
{
var e = document.getElementById(id);
if (e != null)
{
return Math.round(e.value*10000)/10000;
}
return 0;
}
You can probably just return the plain number from the num
Function, however I show it here for completeness!