JavaScript - Having Troubles With Decimals
First problem:
I have a script that allows ONLY numbers to be typed into a text field. I have tried to modify this script to allow Decimals (periods) to be typed as well, but haven't succeeded. The code in Red are the changes I added to this script, but it isn't working out. Code: function numbersOnly(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 110 || charCode > 110) ) return false; return true; } Second Problem: I need to set a Minimum number users may type in the text field. Normally this would be a simple task, but the minimum number they may type is a decimal (0.10). The default value in the field is .50. I tried replacing the .50 with .09, and I can't get the field to run the script and set its new value to .10 (<- this is the minimum). Here is the code I am working with. Code: <?php $memberType = "free"; $creditsBalance = .5; $bannerBalance = 1000; $textBalance = 1000; ?> <html> <head> <title>Convert Credits</title> <style type="text/css"> #convert{ height: 10px; } </style> <script type="text/javascript" language="javascript"> function numbersOnly(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 110 || charCode > 110)) return false; return true; } function convert(){ creditsBanner = Number(document.convertform.creditsbanner.value); creditsText = Number(document.convertform.creditstext.value); bannerText = Number(document.convertform.bannertext.value); textBanner = Number(document.convertform.textbanner.value); memberType = "<?php echo $memberType;?>"; creditsBalance = "<?php echo $creditsBalance;?>"; bannerBalance = "<?php echo $bannerBalance;?>"; textBalance = "<?php echo $textBalance;?>"; if(memberType=="free"){ // If Free // check credit balance if(creditsBanner>creditsBalance){ document.convertform.creditsbanner.value = creditsBalance; creditsBanner = creditsBalance; if(creditsBanner<.1){ document.convertform.creditsbanner.value = 0.1; creditsBanner = 0.1; } } if(creditsText>creditsBalance){ document.convertform.creditstext.value = creditsBalance; creditsText = creditsBalance; if(creditsText<.05){ document.convertform.creditstext.value = 0.05; creditsText = 0.05; } } // check banner balance if(bannerText>bannerBalance){ document.convertform.bannertext.value = bannerBalance; bannerText = bannerBalance; if(bannerText<.5){ document.convertform.bannertext.value = .5; bannerText = .5; } } // check text balance if(textBanner>textBalance){ document.convertform.textbanner.value = textBalance; textBanner = textBalance; if(textBanner<2){ document.convertform.textbanner.value = 2; textBanner = 2; } } crBanner = Number(creditsBanner * 10); crText = Number(creditsText * 20); bnText = Number(bannerText * 2); txBanner = Number(textBanner / 2); document.getElementById("creditstobanner").innerHTML="<b>"+crBanner+"</b>"; document.getElementById("creditstotext").innerHTML="<b>"+crText+"</b>"; document.getElementById("bannertotext").innerHTML="<b>"+bnText+"</b>"; document.getElementById("texttobanner").innerHTML="<b>"+txBanner+"</b>"; } else{ // If Upgraded } } // function updateValues(val) { // document.getElementById('bannerimp').value = val * 10; // document.getElementById('field2').value = val * 20; // document.getElementById('field3').value = val / 2; // document.getElementById('field4').value = val * 2; // } //document.getElementById('creditstobanner').innerHTML </script> </head> <body> <h1>Convert Credits</h1><br /> Credits: <?php echo $creditsBalance; ?><br /> Banner Impressions: <?php echo $bannerBalance; ?><br /> Text Ad Impressions: <?php echo $textBalance; ?><br /> <form action="convert.php" method="POST" name="convertform"> <h2>Convert Your Credits to Banner or Text Ad Impressions</h2> <div id="convert">Convert <input type="text" name="creditsbanner" size="5" onKeyUp="convert();" onKeyPress="return numbersOnly(event);"> Credits to <span id="creditstobanner"><b>0</b></span> Banner Impressions!</div> <br /> <div id="convert">Convert <input type="text" name="creditstext" size="5" onKeyUp="convert();" onKeyPress="return numbersOnly(event);"> Credits to <span id="creditstotext"><b>0</b></span> Text Ad Impressions!</div><br /> <h2>Convert Your Impressions</h2> <div id="convert">Convert <input type="text" name="bannertext" size="5" onKeyUp="convert();" onKeyPress="return numbersOnly(event);"> Banner Impressions to <span id="bannertotext"><b>0</b></span> Text Ad Impressions!</div><br /> <div id="convert">Convert <input type="text" name="textbanner" size="5" onKeyUp="convert();" onKeyPress="return numbersOnly(event);"> Text Ad Impressions to <span id="texttobanner"><b>0</b></span> Banner Impressions!</div><br /> </form> </body> </html> Similar TutorialsHi, First time user of Javascript and built an online calculator, trouble is I dont know where or how to code it so the answer ends with only 2 decimals I have embedded code, help appreciated. Personally I think it's a miracle the darn thing works. Link to page it's on. Any help appreciated, I have searched and read but after hours of trying stuff gave up. <!-- function treevalue() { document.volume.result.value=(((3.14159*((document.volume.radius.value)*(document.volume.radius.valu e)))*(document.volume.height.value))/3)*(document.volume.base.value)*(document.volume.e.value)*(document.volume.fv.value)*(document.volum e.l.value); } // --> Thanks Eric Hi all, I have a problem that has my brain turning to goo... I have a function that mixed with php adds the total numerical valus from different dropdowns. Code: <?php if($z == 1) { ?> <script type="text/javascript"><!-- function updatesum() { document.form.sum.value = <?php echo number_format($prodprice,2)?>+ <?php foreach(range('1',$matchcount2) as $i){?>(document.form.sublist<?php echo $i?>.options[document.form.sublist<?php echo $i?>.selectedIndex].id-0) + <?php }?> (document.form.sublist999.options[document.form.sublist999.selectedIndex].id-0.00); } //--></script> <?php }?> The total is then sent to an input field. Code: <div class="left product_text_box product_title">Total Price: <?php echo $currency_type?><input class="product_title" name="sum" readonly style="background:none; border:none" value="<?php echo number_format($prodprice,2)?>"></div> All works well, but the output allows too many decimals, ie: 46.8966666 I have tried to implement the toFixed(2) with only errors as my reward. Any help would be appreciated KF Hi, I need my javascript to calculate a total with 2x decimal figures, not rounded up. Can you help? Here is my code: Code: <script type="text/javascript"> function calculateText() { var op1=document.getElementById('Age02'); var op2=document.getElementById('Girl35'); var op3=document.getElementById('Boy35'); var op4=document.getElementById('Girl610'); var op5=document.getElementById('Boy610'); var op6=document.getElementById('Age11'); var op7=document.getElementById('Guest'); var op8=document.getElementById('VIP'); var op9=document.getElementById('School'); var op10=document.getElementById('3DFilm'); var op11=document.getElementById('BFast'); var op12=document.getElementById('PNGift'); var op13=document.getElementById('PStand'); var op14=document.getElementById('PLrg'); var op15=document.getElementById('PReP'); var op16=document.getElementById('KyMg'); var op17=document.getElementById('SnwGlb'); var op18=document.getElementById('PBbl'); var op19=document.getElementById('USB'); var op20=document.getElementById('SantaL'); var op21=document.getElementById('SantaK'); var op22=document.getElementById('FreeP'); var op23=document.getElementById('PUpgrd'); var op24=document.getElementById('StaffP'); var op25=document.getElementById('CompEntry'); var op26=document.getElementById('241'); var op27=document.getElementById('50Percent'); var op28=document.getElementById('Voucher'); var result=document.getElementById('Total'); if(op1.value=="" || op1.value!=parseFloat(op1.value)) op1.value=0; if(op2.value=="" || op2.value!=parseFloat(op2.value)) op2.value=0; if(op3.value=="" || op3.value!=parseFloat(op3.value)) op3.value=0; if(op4.value=="" || op4.value!=parseFloat(op4.value)) op4.value=0; if(op5.value=="" || op5.value!=parseFloat(op5.value)) op5.value=0; if(op6.value=="" || op6.value!=parseFloat(op6.value)) op6.value=0; if(op7.value=="" || op7.value!=parseFloat(op7.value)) op7.value=0; if(op8.value=="" || op8.value!=parseFloat(op8.value)) op8.value=0; if(op9.value=="" || op9.value!=parseFloat(op9.value)) op9.value=0; if(op10.value=="" || op10.value!=parseFloat(op10.value)) op10.value=0; if(op11.value=="" || op11.value!=parseFloat(op11.value)) op11.value=0; if(op12.value=="" || op12.value!=parseFloat(op12.value)) op12.value=0; if(op13.value=="" || op13.value!=parseFloat(op13.value)) op13.value=0; if(op14.value=="" || op14.value!=parseFloat(op14.value)) op14.value=0; if(op15.value=="" || op15.value!=parseFloat(op15.value)) op15.value=0; if(op16.value=="" || op16.value!=parseFloat(op16.value)) op16.value=0; if(op17.value=="" || op17.value!=parseFloat(op17.value)) op17.value=0; if(op18.value=="" || op18.value!=parseFloat(op18.value)) op18.value=0; if(op19.value=="" || op19.value!=parseFloat(op19.value)) op19.value=0; if(op20.value=="" || op20.value!=parseFloat(op20.value)) op20.value=0; if(op21.value=="" || op21.value!=parseFloat(op21.value)) op21.value=0; if(op22.value=="" || op22.value!=parseFloat(op22.value)) op22.value=0; if(op23.value=="" || op23.value!=parseFloat(op23.value)) op23.value=0; if(op24.value=="" || op24.value!=parseFloat(op24.value)) op24.value=0; if(op25.value=="" || op25.value!=parseFloat(op25.value)) op25.value=0; if(op26.value=="" || op26.value!=parseFloat(op26.value)) op26.value=0; if(op27.value=="" || op27.value!=parseFloat(op27.value)) op27.value=0; if(op28.value=="" || op28.value!=parseFloat(op28.value)) op28.value=0; result.value=0; result.value=parseInt(result.value); result.value=parseInt(result.value)+parseInt(op1.value*5)+parseInt(op2.value*5)+parseInt(op3.value*5)+parseInt(op4.value*5)+parseInt(op5.value*5)+parseInt(op6.value*5)+parseInt(op7.value*0)+parseInt(op8.value*8.50)+parseInt(op9.value*3.75)+parseInt(op10.value*0)+parseInt(op11.value*10)+parseInt(op12.value*5)+parseInt(op13.value*3)+parseInt(op14.value*4)+parseInt(op15.value*2)+parseInt(op16.value*4)+parseInt(op17.value*5)+parseInt(op18.value*3)+parseInt(op19.value*5)+parseInt(op20.value*4)+parseInt(op21.value*3)+parseInt(op22.value*0)+parseInt(op23.value*1)+parseInt(op24.value*0)+parseInt(op25.value*0)+parseInt(op26.value*4)+parseInt(op27.value*2)-parseInt(op28.value); } </script> Hi this is on my midterm review and I got the program down easily but I just wanted to know if there was an easy way of shortening down the answer to only 2 decimal places.possibly through division? Code: import java.util.*; public class StockSale { public static void main(String[] args) { Scanner input = new Scanner(System.in); double stockSale; // represent the stock sale price double commission; System.out.print("Please enter a the stock value price: "); stockSale = input.nextDouble(); if(stockSale < 100.00) commission = 20.00; else if (stockSale <= 999.99) commission = (20.00 + ((stockSale - 99.99)*.01)); else if (stockSale <= 9999.99) commission = (30.00 + ((stockSale - 999.99)*.005)); else if (stockSale <= 99999.99) commission = (75.00 + (( stockSale - 9999.99)*.0025)); else commission = ( 150.00 + ((stockSale - 99999.99)*.0015)); System.out.print("The commission on this stock sale is $" + commission ); } } i appreciate any help given thanks How would I go about creating a random number between 1-50 that COULD include whole numbers, decimals to the tenth, hundredth and the thousandth place? Ex) 2.145, 3, 5.8, 41.002, 10.12, 7, 18.023, 33.2 Also, if the only way to do it is to put trailing zeros to the thousandth place that's fine as well ex) 2.145, 3.00, 5.800, 41.002, 10.120, 7.00, 18.023, 33.200 Hi, my javascript calculates a total figure. It rounds it up though and I want it to give me a decimal (x2) total. Please help. Here is my code so far: Code: <script type="text/javascript"> function calculateText() { var op1=document.getElementById('Age02'); var op2=document.getElementById('Girl35'); var op3=document.getElementById('Boy35'); var op4=document.getElementById('Girl610'); var op5=document.getElementById('Boy610'); var op6=document.getElementById('Age11'); var op7=document.getElementById('Guest'); var op8=document.getElementById('VIP'); var op9=document.getElementById('School'); var op10=document.getElementById('3DFilm'); var op11=document.getElementById('BFast'); var op12=document.getElementById('PNGift'); var op13=document.getElementById('PStand'); var op14=document.getElementById('PLrg'); var op15=document.getElementById('PReP'); var op16=document.getElementById('KyMg'); var op17=document.getElementById('SnwGlb'); var op18=document.getElementById('PBbl'); var op19=document.getElementById('USB'); var op20=document.getElementById('SantaL'); var op21=document.getElementById('SantaK'); var op22=document.getElementById('FreeP'); var op23=document.getElementById('PUpgrd'); var op24=document.getElementById('StaffP'); var op25=document.getElementById('CompEntry'); var op26=document.getElementById('241'); var op27=document.getElementById('50Percent'); var op28=document.getElementById('Voucher'); var result=document.getElementById('Total'); if(op1.value=="" || op1.value!=parseFloat(op1.value)) op1.value=0; if(op2.value=="" || op2.value!=parseFloat(op2.value)) op2.value=0; if(op3.value=="" || op3.value!=parseFloat(op3.value)) op3.value=0; if(op4.value=="" || op4.value!=parseFloat(op4.value)) op4.value=0; if(op5.value=="" || op5.value!=parseFloat(op5.value)) op5.value=0; if(op6.value=="" || op6.value!=parseFloat(op6.value)) op6.value=0; if(op7.value=="" || op7.value!=parseFloat(op7.value)) op7.value=0; if(op8.value=="" || op8.value!=parseFloat(op8.value)) op8.value=0; if(op9.value=="" || op9.value!=parseFloat(op9.value)) op9.value=0; if(op10.value=="" || op10.value!=parseFloat(op10.value)) op10.value=0; if(op11.value=="" || op11.value!=parseFloat(op11.value)) op11.value=0; if(op12.value=="" || op12.value!=parseFloat(op12.value)) op12.value=0; if(op13.value=="" || op13.value!=parseFloat(op13.value)) op13.value=0; if(op14.value=="" || op14.value!=parseFloat(op14.value)) op14.value=0; if(op15.value=="" || op15.value!=parseFloat(op15.value)) op15.value=0; if(op16.value=="" || op16.value!=parseFloat(op16.value)) op16.value=0; if(op17.value=="" || op17.value!=parseFloat(op17.value)) op17.value=0; if(op18.value=="" || op18.value!=parseFloat(op18.value)) op18.value=0; if(op19.value=="" || op19.value!=parseFloat(op19.value)) op19.value=0; if(op20.value=="" || op20.value!=parseFloat(op20.value)) op20.value=0; if(op21.value=="" || op21.value!=parseFloat(op21.value)) op21.value=0; if(op22.value=="" || op22.value!=parseFloat(op22.value)) op22.value=0; if(op23.value=="" || op23.value!=parseFloat(op23.value)) op23.value=0; if(op24.value=="" || op24.value!=parseFloat(op24.value)) op24.value=0; if(op25.value=="" || op25.value!=parseFloat(op25.value)) op25.value=0; if(op26.value=="" || op26.value!=parseFloat(op26.value)) op26.value=0; if(op27.value=="" || op27.value!=parseFloat(op27.value)) op27.value=0; if(op28.value=="" || op28.value!=parseFloat(op28.value)) op28.value=0; result.value=0; result.value=parseInt(result.value); result.value=parseInt(result.value)+parseInt(op1.value*5)+parseInt(op2.value*5)+parseInt(op3.value*5)+parseInt(op4.value*5)+parseInt(op5.value*5)+parseInt(op6.value*5)+parseInt(op7.value*0)+parseInt(op8.value*8.50)+parseInt(op9.value*3.75)+parseInt(op10.value*0)+parseInt(op11.value*10)+parseInt(op12.value*5)+parseInt(op13.value*3)+parseInt(op14.value*4)+parseInt(op15.value*2)+parseInt(op16.value*4)+parseInt(op17.value*5)+parseInt(op18.value*3)+parseInt(op19.value*5)+parseInt(op20.value*4)+parseInt(op21.value*3)+parseInt(op22.value*0)+parseInt(op23.value*1)+parseInt(op24.value*0)+parseInt(op25.value*0)+parseInt(op26.value*4)+parseInt(op27.value*2)-parseInt(op28.value); } </script> Hello everyone, I'm in a bit of a pickle. Here are all the details and I hope someone will be able to help me out... I have a CD; the way this CD installs is...when inserted, the CD runs a file Login.html...on this page there are 3 fields, login, password, and language (depending on which language you click on, that's the language that it automatically changes the Login and Password text to. The username and password need to be an account that is on a server that the login.html connects to. Once you log in, the system automatically bring you to the next page which is also located on the CD which is Install.html...now on this page, there are 4 fields. Destination of where I want to install, Language drop down, but here's the tricky part...the two other fields are Currency and Sales Origination. These two fields get auto populated depending on the language we selected on the first page, Login.html. but the trick is that if I bypass the Login.html file and point my browser directly to the Install.html file, the Currency and Sales Origination fields will be blank and I will not be able to install the application. Keep in mind, this is not an exe or MSI installation, it's a batch file that's on the CD and it performs several tasks such as moving files, extracting files, placing things in scheduled tasks, and a few other things. So what I need to do is figure out a way, hopefully with your guys help, is how to bypass the Login.html altogether so that I can run Install.html without needing anything from the Login.html. So I need to, I'm guessing, edit the Install.html file so that it already has Currency and Sales Origination pre-populated. Because if I go to Install.html directly and leave those two fields blank, the batch file that runs to install everything, it says there is no Currency file that is selected. Because the way it's created is it selects a certain .zip file from the CD and extracts it depending/according to the Currency and Sales Originations...which that depends all on the Language that was selected on the Login.html page. Currency: ZPF Sales Origination: 02CA these are the values that it's supposed to have. Hopefully this gives you guys an idea, because unfortunately I don't know much about JavaScript, so looking for some help from the pros I wasnt sure where to put this one, since its mainly javascript I thought I would put it here. I am trying to pull a value being stored in a php function (it works fine) which is echoed into an html element, and use it in a javascript function. my HTML: Code: <img id="menu_image" src="<?php echo get_php_value(); ?>" width="356" /> my javascript: Code: function get_large_image(){ var image = document.getElementById('menu_image').src; alert(image); } right now the alert is giving me flie path to the current page, I need a file path to the image. When I had javascript pull a value from an input tag using the id to reference and stored the php function in the value attribute, I got this: "object HtmlElement" The final purpose of all this is to modify dynamically and HTML <a>'s href and class attribute based on a certain condition being true. apprecaite the help Hey guys, Here is my question: Is it possible to extract a cookie inside a textarea? For example, I am building a very basic shopping cart in Javascript, with "Add to Cart" buttons listed for each item. As the "Add to Cart" button is pressed, a cookie is created. There is a link in which you can click with "Review Order", which consists of a textarea that displays the items that you have 'purchased'. I am trying to get the value of the cookie to be extracted into this textarea, however I am not sure if this is possible. Thanks |