Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving Value from a textbox

Status
Not open for further replies.

CowboyFanTech

Programmer
Sep 19, 2008
5
US
I'm having a problem with retrieving a value from a text box and modifying it while being to display it in a window.alert box. I can multiply the value; but, if i want to add anything else it (javascript) will only concatenate the result. Here is my code for the function:

<script type="text/javascript">
/* <![CDATA[*/
function ShippingCharge(form, form01) {
var shipping001 = document.getElementById('shipp001').value;
var ShippingsubTot = document.getElementById('shipp001').value;
var ShippingMinimum = 250.00;
var ShippingRate = .10;
var AdditionalShipping = .15;
var ShippingTotal = 0;
var ShippingTotal001 = 0;
var Display01 = "Shipping Charge will be: $";
var Display02 = "Total Charge including shipping will be: $";

if (shipping001 >= ShippingMinimum) {

ShippingTotal001 = (parseInt(shipping001.value) * AdditionalShipping) + parseInt(ShippingsubTot.value);

return window.alert("" + Display02 + "" + ShippingTotal001 + "");
}
else {

ShippingTotal = shipping001 * ShippingRate;
ShippingTotal += shipping001;


return window.confirm("Shipping charge will be: $" + ShippingTotal + " ");
}
}


This has to be a simple fix.

Thanx,

CowboyfanTech
 
Thank you.

How do you get the two decimal places for a monetary value?


CowboyFanTech
 
If you are looking for a way to format a number into currency, here is a function that I always use:
Code:
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num))
		num = "0";
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;
		
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
		
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
 
I'm trying to retrieve a monetary value from a text box in html. First, it will multiply the value but after that it will concatenate like a string.

CowboyFanTech
 
I used the .toFixed(2) method to solve the monetary problem.

CowboyFanTech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top