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
    Regular Coder
    Join Date
    Mar 2003
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow code help useability

    Hi i'm put together this code and i newish to JS, could I have some pointers on the code, things pointed out that could cause problems, with browser compatibility, or just smart things to do, Thanks

    <SCRIPT LANGUAGE="javascript">
    function addit() {
    var a21 = document.form1.bi3.value;
    a21 = (a21 * 14.95 + <%=c1%>);
    a21 = "" + ((Math.round(a21 * 100)) / 100);
    dec1 = a21.substring(a21.length-3, a21.length-2);
    dec2 = a21.substring(a21.length-2, a21.length-1);
    if (dec1 != '.') {
    if (dec2 == '.') a21 += "0";
    else a21 += ".00";
    }
    if (isNaN(a21) || a21==""){
    document.getElementById("vv").innerHTML = "Invalid Number";
    document.form1.bi3.value = 0
    } else {
    document.getElementById("vv").innerHTML = "$" + a21;
    }
    }
    </SCRIPT>


    </head><body...
    <span style="font-weight: 700; background-color: #FFFF00"><div id="vv">$<%=c1%></div></span>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts
    subjective

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    </head>
    
    <SCRIPT LANGUAGE="javascript">
     var a21;
    
    function addit() {
    var a21 = document.form1.bi3.value;
     if (isNaN(a21)|| a21==""){
      // do first to stop calculations on NaN
      obj.value = 0
      document.getElementById("vv").innerHTML = "Invalid Number";
      return;
     }
     a21 = Number(a21); // ? better than auto typing
     a21 = (a21 * 14.95 + 22);
     a21 = "" + ((Math.round(a21 * 100)) / 100);
     dec1 = a21.substring(a21.length-3, a21.length-2);
     dec2 = a21.substring(a21.length-2, a21.length-1);
     if (dec1 != '.') {
     if (dec2 == '.') a21 += "0";
      else a21 += ".00";
     }
     document.getElementById("vv").innerHTML = "$"+a21;
    }
    
    function addit2(obj) {
     a21 = obj.value;
     if (isNaN(a21)|| a21==""){
      // do first to stop calculations on NaN
      obj.value = 0
      document.getElementById("vv").innerHTML = "Invalid Number";
      return;
     }
     a21 = Number(a21); // ? better than auto typing
     a21 = (a21 * 14.95 + 22);
     a21s=a21.toString().split('.');
     if (a21s.length<2){
      a21s[1]='00';
     }
     a21=Number((a21s[0]+'.'+a21s[1])).toFixed(2);
     document.getElementById("vvvvv").innerHTML = "$"+a21.toString();
    }
    
    </SCRIPT>
    
    
    </head>
    <body>
    <form name=form1 >
    <div id="vv"   ></div>
    <input name="bi3" size="10" onblur="a21=this.value;addit(this);" >
    <br><br>
    Alternative<br>
    <div id="vvvvv"   ></div>
    <input name="" size="10" onblur="addit2(this);" >
    </form>
    </body>
    
    </html>


  •  

    Posting Permissions

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