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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing javascript string

Status
Not open for further replies.

lahddah

Programmer
Jan 2, 2003
109
US
Hi. Can someone show me how to correctly parse this form field name so that '.19' ends up being calculated as weight for shipping purposes?

The field is this:
Qty:<INPUT TYPE="TEXT" NAME="PROD-WAovalCap_.19-16.95" SIZE="3" MAXLENGTH="3" ONCHANGE="CalculateTotal(this.form)" VALUE="">

The script portion is here:
item_weight = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

-------------
item_weight should be .19 X item_quantity, but I'm either getting NaN as a result or '.5'

I am successfully parsing out '16.95' as the price, but cannot figure out how to get the '.19' out.

Hope that makes sense. I can publish the whole thing, but it was lengthy.

Thanks!


~ lahddah
 
Try this method. Strip the weight component out of the name using the [tt]slice[/tt] method, then use [tt]parseFloat[/tt] to get the numeric value:

Code:
<html>
<head>
<script>
function parseWeight(objCurrentField){
  var strTest = objCurrentField.name;
  var strWeight = strTest.slice(strTest.lastIndexOf("_") + 1, strTest.lastIndexOf("-"));
  var floatWeight = parseFloat(strWeight);
  return floatWeight;
}
</script>
</head>
<body>
<INPUT TYPE="TEXT" NAME="PROD-WAovalCap_.19-16.95" SIZE="3" MAXLENGTH="3" ONCLICK="alert(parseWeight(this))" VALUE="">
</body>
</html>


Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
is . allowed in a name of a field?

Known is handfull, Unknown is worldfull
 
That was a question I had, too, vbkris. It looks like it is functioning evening with the '.' in the string. I am still having a hard time with calculating the shipping on this page, though.

dwarfthrower - your example seems to have worked - THANK YOU! Now, I would like the weight to accumulate for each item chosen and the shipping will be based on the overall weight. So if the weight is < .6 lbs, then the shipping would be $4.95. If it is less then 1.2 lbs, then the shipping would be $7.95, and so on according to my little chart on this example page.


The script has some alert msgs I was using for testing so that I can keep track of what the weight is.

Here's the script:
-------
<SCRIPT>
function parseWeight(objCurrentField){
var strTest = objCurrentField.name;
var item_qty = objCurrentField.value;
var strWeight = strTest.slice(strTest.lastIndexOf("_") + 1, strTest.lastIndexOf("-"));
var floatWeight = parseFloat(strWeight) * item_qty;

return floatWeight;

}
</SCRIPT><SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--

/* This script is Copyright (c) Paul McFedries and
Logophilia Limited (Permission is granted to use this script as long as
this Copyright notice remains in place.*/

function CalculateTotal(frm) {
var order_total = 0
var order_shipping = 0
var order_tax = 0
//var item_weight = 0
frm.shipping495.checked = false;
frm.shipping795.checked = false;
frm.shipping1195.checked = false;
frm.shipping1495.checked = false;
frm.shipping1795.checked = false;


// Run through all the form fields
for (var i=0; i < frm.elements.length; ++i) {

// Get the current field
form_field = frm.elements

// Get the field's name
form_name = form_field.name

// Is it a "product" field?
if (form_name.substring(0,4) == "PROD") {

// If so, extract the price from the name
item_price = parseFloat(form_name.substring(form_name.lastIndexOf("-") + 1))


// Get the quantity
item_quantity = parseInt(form_field.value)
item_weight = item_weight + parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

//alert ("The weight is "+item_weight+ " poundz");
return item_weight;

// Update the order tota
if (item_quantity >= 0) {
order_total += item_quantity * item_price;
if (item_weight <= .6 ) {
frm.shipping495.checked = true;
order_shipping = parseFloat(frm.shipping495.value);
}
else if (item_weight < 1.2 ) {
frm.shipping795.checked = true;
order_shipping = parseFloat(frm.shipping795.value);
}
else if (item_weight < 1.8 ) {
frm.shipping1195.checked = true;
order_shipping = parseFloat(frm.shipping1195.value);
}
else if (item_weight < 3.6 ) {
frm.shipping1495.checked = true;
order_shipping = parseFloat(frm.shipping1495.value);
}
else
{
frm.shipping1795.checked = true;
order_shipping = parseFloat(frm.shipping1795.value);
}
}
}
}

// Display the SUB total rounded to two decimal places
frm.TOTAL.value = round_decimals(order_total, 2)
frm.TOTAL1.value = round_decimals(order_total, 2)
frm.GrandTotal.value = round_decimals(order_total + order_shipping, 2);
frm.shipping.value = round_decimals(order_shipping, 2)


// Display the FULL total rounded to two decimal places
// frm.TOTALFULL.value = round_decimals((order_total + order_shipping), 2);
//if (frm.AddTax.checked == true) {
//order_tax = parseFloat(0.05 * (order_total + order_shipping));
//}
//frm.GST.value = round_decimals(order_tax, 2);
//frm.GrandTotal.value = round_decimals(order_total + order_shipping + order_tax, 2);
}

function round_decimals(original_number, decimals) {
var result1 = original_number * Math.pow(10, decimals)
var result2 = Math.round(result1)
var result3 = result2 / Math.pow(10, decimals)
return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

// Convert the number to a string
var value_string = rounded_value.toString()

// Locate the decimal point
var decimal_location = value_string.indexOf(".")

// Is there a decimal point?
if (decimal_location == -1) {

// If no, then all decimal places will be padded with 0s
decimal_part_length = 0

// If decimal_places is greater than zero, tack on a decimal point
value_string += decimal_places > 0 ? "." : ""
}
else {

// If yes, then only the extra decimal places will be padded with 0s
decimal_part_length = value_string.length - decimal_location - 1
}

// Calculate the number of decimal places that need to be padded with 0s
var pad_total = decimal_places - decimal_part_length

if (pad_total > 0) {

// Pad the string with 0s
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}


//-->
</SCRIPT>

-----------------
And an example of the QTY box:
-----------------

<INPUT TYPE="TEXT" NAME="PROD-Microfleece_.63-39.95" SIZE="3" MAXLENGTH="3" ONCHANGE="alert(parseWeight(this));CalculateTotal(this.form)" VALUE="">




Hope that helps!
Thanks, again!



~ lahddah
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top