CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2002
    Posts
    22

    Unhappy pplease help me-with this part of code for a cart

    here is the code for my cart. it is weird, i dont want "Decimal" quantity multiplying the price. how do change that so if someone inputs 1.5 as quantity it five sout an error. here is the code:


    function MakeTen(expr,decimal){

    var str="" + Math.round(eval(expr) * Math.pow(10,decimal))

    while (str.length <= decimal){

    str = "0" + str

    }

    var decpoint = str.length - decimal

    return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);

    }



    function MakeTwo(expr){

    return "$" + MakeTen(expr,2)

    }



    function cartMe() {

    index = document.cookie.indexOf("MyCart");

    countbegin = (document.cookie.indexOf("=", index) + 1);

    countend = document.cookie.indexOf(";", index);

    if (countend == -1) {

    countend = document.cookie.length;

    }

    fulllist = document.cookie.substring(countbegin, countend);

    totprice = 0;

    document.write('<TABLE BORDER=1 CELLSPACING=2 CELLPADDING=2 BGCOLOR=adcfdc>');

    document.write('<TR><TD><b>Qty</b></TD><TD><b>Product</b></TD><TD><FONT COLOR="#FFFFFF">.</FONT></TD><TD><b>Price</b></TD><td><b>Sub total</b></TD><TD><FONT COLOR="#FFFFFF">.</FONT></TD></TR>');

    itemlist = 0;

    for (var i = 0; i <= fulllist.length; i++) {

    if (fulllist.substring(i,i+1) == '[') {

    itemstart = i+1;

    }

    else if (fulllist.substring(i,i+1) == ']') {

    itemend = i;

    thequantity = fulllist.substring(itemstart, itemend);

    itemtotal = 0;

    itemtotal = (eval(theprice*thequantity));

    temptotal = itemtotal * 100;

    totprice = totprice + itemtotal;

    itemlist=itemlist+1;

    document.write('<tr><td align=middle>'+thequantity+'</td><td align=left>'+theitem+'</td><td align=right>'+theoption+'</td><td align=right>$'+theprice+'</td><td align=right>'+top.menu.MakeTwo(itemtotal)+'</td><td><a href="javascript:removeItem('+itemlist+')">Remove</a></td></tr>');

    }

    else if (fulllist.substring(i,i+1) == ',') {

    theitem = fulllist.substring(itemstart, i);

    itemstart = i+1;

    }

    else if (fulllist.substring(i,i+1) == '^') {

    theprice = fulllist.substring(itemstart, i);

    itemstart = i+1;

    }

    else if (fulllist.substring(i,i+1) == '~') {

    theoption = fulllist.substring(itemstart, i);

    itemstart = i+1;

    }

    }

    document.write('<tr><td align=right colspan=4><b>Total</b></td><td align=right>'+top.menu.MakeTwo(totprice)+'</td><TD><FONT COLOR="#FFFFFF">.</FONT></TD></tr>');

    document.write('</TABLE>');

    }

  2. #2
    Join Date
    Aug 2002
    Location
    Washington, USA
    Posts
    104
    I don't understand the question. Do you not want the user to be able to enter quantities that are not whole numbers? Or do you not want to format the price as a 2-place decimal ($0.00)?
    - Shawn
    MCP, VB6: Desktop Apps
    [ C# | VB | .NET | Java | VC++ | Perl | PHP | Javascript ]
    Unless otherwise stated, all sample code provided is UNTESTED.
    http://www.codemastershawn.com

  3. #3
    Join Date
    Nov 2002
    Posts
    22
    I dont want the customer enter like 1.5 as quantity...quantity has to be a whole number compare to the price. Do you now what i am tlaking about.

  4. #4
    Join Date
    Aug 2002
    Location
    Washington, USA
    Posts
    104
    Originally posted by jinthu200
    I dont want the customer enter like 1.5 as quantity...quantity has to be a whole number compare to the price. Do you now what i am tlaking about.
    Yes. But the code that handles user input is not part of what you provided. You will have to add code there (not here) to report an error and disallow the entry of fractional numbers.
    - Shawn
    MCP, VB6: Desktop Apps
    [ C# | VB | .NET | Java | VC++ | Perl | PHP | Javascript ]
    Unless otherwise stated, all sample code provided is UNTESTED.
    http://www.codemastershawn.com

  5. #5
    Join Date
    Nov 2002
    Posts
    22
    <TD ALIGN="RIGHT" width="49" height="122">$49.00</TD>
    <TD width="29" height="122">
    <INPUT TYPE="TEXT" NAME="he002quantone1" VALUE="1" SIZE="3"></TD>
    <TD width="100" height="122">
    &nbsp;<i><INPUT TYPE="BUTTON" NAME="he001add1" VALUE="Add to Cart" ONCLICK="top.menu.buyItem('Black Handbag','49.00', document.itemsform.he001quantone.value, document.itemsform.he001option.value)"><TR ALIGN=Left VALIGN=Bottom>
    <TD width="72" height="25" valign="top">
    <a href="images/blue.jpg">
    <img border="0" src="images/blue.jpg" width="146" height="218"></a></TD>

  6. #6
    Join Date
    Aug 2002
    Location
    Washington, USA
    Posts
    104
    function ValidateForm()
    {
    // if (document.itemsform.he001quantone.value is fractional)
    &nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;alert("You must enter a whole number");
    &nbsp;&nbsp;&nbsp;&nbsp;return false;
    &nbsp;&nbsp;}
    //else
    &nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;return true;
    &nbsp;&nbsp;}
    }
    ...
    <form name="itemsform" ... onSubmit="return ValidateForm();">
    - Shawn
    MCP, VB6: Desktop Apps
    [ C# | VB | .NET | Java | VC++ | Perl | PHP | Javascript ]
    Unless otherwise stated, all sample code provided is UNTESTED.
    http://www.codemastershawn.com

  7. #7
    Join Date
    Nov 2002
    Posts
    90
    I assume that you have a text box field named for example 'txtQty', you can use a function like this to make sure the user doesn't enter decimal value (1.5 for instance) as quantity:
    function validNumbers(sStr)
    {
    var sValidNumbers = "1234567890"
    var bReturn = true
    var i = new Number(0);
    while ((bReturn) && (i < sStr.length))
    {
    bReturn = (sValidNumbers.indexOf(sStr.charAt(i)) >= 0)
    i++
    }
    return (bReturn)
    }

    Here is the way you can call the function:

    if (validNumbers(txtQty)==false))
    {alert("Invalid Quantity);}

    This function will make sure that the user enter a valid quantity which is a decimal value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured