SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member davestott1's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Decimalise Form Field Using JS

    I have the code below on a page I am building

    Basically what I want to do is to have the last total field (with discounted price) show the value with 2 decimal places (for example 2.25 instead of 2.2535456)

    Is this possible using JavaScript and if so could anyone point me in the right direction?

    Thanks

    Dave

    (my code follows>)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript">
    function discounter() {
    document.form1.total.value=document.form1.itemprice.value/100*10
    document.form1.discountedprice.value=document.form1.itemprice.value-document.form1.total.value
    }
    </script>
    </head>

    <body>
    <form name="form1" method="post" action="">
    <p>Item Price=
    <input name="itemprice" type="text" onChange="return discounter()" id="itemprice" size="5">
    </p>
    <p> With Discounted Price=
    <input name="discountedprice" type="text" disabled id="discountedprice3">
    <input name="total" type="hidden" disabled id="total">
    </p>
    </form>
    </body>
    </html>

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ....tossed in a couple of extras
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript">
    
    function discounter(oForm)
    {
    	if (isNaN(oForm.itemprice.value))
    	{
    		alert('Please enter a valid price.');
    		oForm.itemprice.focus();
    		oForm.itemprice.select();
    		return false;
    	}
    	else
    	{
    		oForm.total.value = oForm.itemprice.value / 100 * 10;
    		oForm.discountedprice.value = $format(oForm.itemprice.value - oForm.total.value);
    	}
    } 
    
    function $format(num)
    {
    	var $str = String(Math.round(num * 100) / 100);
    	var hasdot = ($str.indexOf('.') != -1);
    	$str = (hasdot) ? $str + '00' : $str + '.00';
    	return ' $' + $str.substring(0, $str.indexOf('.') + 3);
    }
     
    </script>
    </head>
    
    <body>
    <form name="form1" method="post" action="" onreset="itemprice.focus()">
    <input name="itemprice" type="text" onblur="return discounter(this.form)" id="itemprice" size="8" />______<b>Item Price</b>
    <br /><br />
    <input name="discountedprice" type="text" readonly="readonly" size="8" />______<b>With Discounted Price</b>
    <input name="total" type="hidden" id="total" />
    <br /><br />
    <input type="reset" />
    </form>
    </body>
    </html>
    Disabling a hidden field is pretty unusual; assume you wanted the readout field as "read-only", typical.
    ::: certified wild guess :::

  3. #3
    SitePoint Member davestott1's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Adios, that's fantastic!!! Thanks very much for helping me out with that

    Just one quick question - could you tell me how to change the currency from $ to ??

    Thanks very much for your help

  4. #4
    SitePoint Member davestott1's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorted it now - thanks for your help though


Bookmarks

Posting Permissions

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