Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    adding up parts etc

    ok this is what i have
    [code]

    <script type="text/javascript">
    <!--

    function calc(){
    // Gathers field information
    var f1 = validNum(document.productForm.elements['tp1'].value);
    var f2 = validNum(document.productForm.elements['tp2'].value);
    var f3 = validNum(document.productForm.elements['tp3'].value);
    var f4 = validNum(document.productForm.elements['tp4'].value);
    var f5 = validNum(document.productForm.elements['tp5'].value);
    var f6 = validNum(document.productForm.elements['labour'].value);
    var deposit = validNum(document.productForm.elements['deposit'].value);
    var prevp = validNum(document.productForm.elements['prevp'].value);



    // Calculates Information
    var subtotal = f1 + f2 + f3 + f4 + f5 + f6;
    var vat = (0.175 * subtotal);
    var total = vat + subtotal;
    var balo = total - deposit
    var balp = total - prevp
    var totalparts = f1 + f2 + f3 + f4 + f5;

    // Post totals
    document.productForm.elements['totalparts'].value = totalparts;
    document.productForm.elements['subtotal'].value = subtotal;
    document.productForm.elements['vat'].value = vat;
    document.productForm.elements['total'].value = total;
    document.productForm.elements['balo'].value = balo;
    document.productForm.elements['balp'].value = balp;

    }

    // Makes sure variable is a number
    function validNum(nmbr){
    // forces variable into integer type
    nmbr = (nmbr * 1);

    // Checks if variable is Not a Number
    if(!isNaN(nmbr))
    // If a number, return value
    return nmbr;
    else
    // If not a number, return zero
    return 0;
    }
    -->
    </script>
    [code/]

    heres an example http://www.ipaul.co.uk/example.html

    these are the problems i got ( well the main problems)

    as you add numbers into total price colums it adds but untill you get to the deposit colum then it refuses to add up unless you go back to the total price colum and re-enter the figure.
    Also i would like the figure to round up to two decimals ie 3.25 etc

    i was wondering if an calculate button would be easier that just lets someone enter the figures and then once they hit the calculate buttons it does the sums.
    If anyone has some example code for this i would be most thankful.


    one other thing i would like to add aswell is if the amount is over a sum say 150 and no repair code is entered into the "REPAIR CODE" box then the form errors notifying that no authority code has been entered into "AUTHORITY NUMBER" box, but if the amount is under 150 no error accurs.

    I know i'm asking alot any pointers are helpfull.

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    1) Please fix your HTML errors.

    2) You seem to have divitis and span-mania. I suggest you learn how semantics apply to X/HTML.
    The Meaning of Semantics Take II: Naming Conventions for Class and ID in CSS
    Use class with semantics in mind

    3) Don't forget to validate those totals on the server.

    4) You forgot to put an onkeyup handler on #deposit.

    5) Use toFixed() to keep two decimal places.
    Code:
    if(typeof(Number)!='undefined'&&typeof(Number.prototype)!='undefined'){
      if(typeof(Number.prototype.toFixed)=='undefined'){
      // for IE versions older than 5.5 and Netscape 4.x. Early versions of NS4.x 
      // don't support ===, so I used == and typeof() to simulate it.
        Number.prototype.toFixed=function(d){
          var n=this;
          d=(d||((d==0)&&(typeof(d)=='number')))?d:2;
          var f=Math.pow(10,d);
          n=((Math.round(n*f)/f)+Math.pow(10,-(d+1)))+'';
          return n.substring(0,(d==0)?n.indexOf('.'):n.indexOf('.')+d+1);
        }
      }
    }
    6) I suggest you read these:
    The Behavior Layer
    Separating behavior and structure
    Behavioral Separation
    Unobtrusive JavaScript
    DOM Scripting - Sample chapter: Best Practices
    Accessible DHTML

    P.S. Please read Guidelines and Suggestions for Posting on Web Development Forums.
    Last edited by Kravvitz; 10-15-2006 at 08:19 PM.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.


  •  

    Posting Permissions

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