JavaScript - How Can I Mod This To Accept (.) Decimal Place
Hi All,
Im currently working on a bidding application built within ASP.Net and i have found this javascript snippet to only allow numeric values in the desired <asp:textbox> which is working fine, but i want to allow the user to add a decimal place. For example, say the bid is 1.50 they should be allowed to enter 1.51 but as i cant imput (.) i end up putting in 151 heres the snippet it may be a slight modification but im new to javascript so any help of knowledge will be highly appreciated Code: function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } thanks in advance Similar TutorialsI have a mortgage calc that is adding an extra decimal place to my interest rate. To see this in action go to: http://www.keithknowles.com/elmtree/mortgagecalc2.htm Change the interest rate to 7.5 Click Calculate Payment Click Create Amortization Chart On the following screen you'll see a recap of your data and the interest rate will now say 7.55. Same thing happens if you enter 6.2. Next screen shows 6.22/ I've attached the html and js files in a zip. Any help is greatly appreciated. Thanks. Hey everyone, I am attempting to use some code from the website Dynamic Drive and the page can be found here. http://www.dynamicdrive.com/dynamici...acceptterm.htm Basically it is javascript code that requires people to check a box before being able to submit a forum. I implemented the code into my site just fine. However I want to be able to use a image submit button rather than a submit button. Since I am not proficient with Javascript I have no idea where to go from here or how to change it to work with an image/paypal button. Here is the code I have so far. Script at top of the page Code: <script> //"Accept terms" form submission- By Dynamic Drive //For full source code and more DHTML scripts, visit http://www.dynamicdrive.com //This credit MUST stay intact for use var checkobj function agreesubmit(el){ checkobj=el if (document.all||document.getElementById){ for (i=0;i<checkobj.form.length;i++){ //hunt down submit button var tempobj=checkobj.form.elements[i] if(tempobj.type.toLowerCase()=="submit") tempobj.disabled=!checkobj.checked } } } function defaultagree(el){ if (!document.all&&!document.getElementById){ if (window.checkobj&&checkobj.checked) return true else{ alert("Please read/accept terms to submit form") return false } } } </script> Form Code: <form name="agreeform" onSubmit="return defaultagree(this)" action="https://urlhere.com" method="post"> <input name="agreecheck" type="checkbox" onClick="agreesubmit(this)"><b>I agree to the above terms</b><br> <input type="Submit" value="Submit!" disabled> <!--<input type="image" src="https://www.webpage.com" border="0" name="I2" alt="PayPal - The safer, easier way to pay online!">--> </form> <script> //change two names below to your form's names document.forms.agreeform.agreecheck.checked=false </script> Please note that the paypal button is commented out in this code as it doesn't work right now and the submit button works. I think the following code needs to be changed to a different check to check for a specific paypal button but I am not sure how to do it. Keep in mind I have four different paypal buttons on the same page so picking just this one is important. Code: if(tempobj.type.toLowerCase()=="submit") Any help with this would be greatly appreciated! Regards, Frank I cannot get an address entered on a form to validate unless I leave out the space between the number and the street name. The second line of the function shows the characters that it will accept. Every way I tried to add a space to that list doesn't work. function isAlphanumeric(elem, helperMsg){ var alphaExp = /^[0-9a-zA-Z]+$/; if(elem.value.match(alphaExp)){ return true; }else{ alert(helperMsg); elem.focus(); return false; } } (This is from a tutorial I'm studying to learn how to do validation. It seems strange that the folks who put this thing together don't know there's a space between the house number and the street name. Or does that only apply to my street?) Any ideas, J I'm probably missing something simple here. I have this function called display: function display(name3,office3,officePH3,mobile3,email3) { document.getElementById('viewer').src=('Location_Files/')+(office3)+('.htm') } It's supposed to take 5 values and do various things with them. When I only had one parameter it worked fine, it took an office number which was the value of a listbox option and turned it into a path and then pointed an iframe to that path. Then I changed it so the value of the listbox option was 5 parameters separated by commas. Here's the listbox now: <select onchange="display(this.value)" multiple="multiple" id="People" name="People" style="border-style: none; height:260px; width:220px;"> <option value="test name,1656,phone,NONE,email">Loading</option> </select> You can see display is executed as "display(this.value)" so in theory it should take "test name,1656,phone,NONE,email" as its parameters and it should accept 1656 as the "office3" parameter. For some reason it's not working out that way, the function executes and the iframe changes but I get an unable to display webpage message like the path is broken. Can anyone see what I'm doing wrong? The way I have my code now inorder to create .XX places I use Code: var valueY=Math.round(valueX*100)/100; That way if valueX=15.6898 it would give valueY=15.69. but the problem arises when I want it valueX=15.6 to display valueY=15.60 I did some research and people said to use .toFixed(2) but i can't get it to add in the zero if it isn't already there. Any help I would be very helpful. Dear All, I can check now a field is isNumeric via this method var isNumeric = /^[0-9.]+$/;. My problem now user can put two or more decimal and I want to limit just to single decimal and only 2 numbers before the decimal? How to edit please? Hi.. I have javascript code with computation, but I have problem in my result because it was not round off or fixed into two decimal places only. here is my code: Code: <script type="text/javascript"> function test_(cmpd) { var len_ = (document.frmMain.elements.length) - 1; for (i=0; i <= len_; i++ ) { var strPos_ = document.frmMain.elements[i].id.indexOf(cmpd) if (strPos_ != -1) { var strPos = document.frmMain.elements[i].id.indexOf("_"); var strId = document.frmMain.elements[i].id.slice(strPos + 1) + "_" + document.frmMain.elements[i].id.slice(0,strPos) // this is the computation that I need to fixed the result into two decimal places document.frmMain.elements[i].value = document.getElementById(strId).value * document.getElementById('mult').value; } } } </script> Thank you so much This script is for car loan payments. I need a R simbol only before the monthly payment and only two figures after the decimal point. Can somebody pse help me with this. Thank you in advance. Kees Meyer (keesmarcha) <script language="JavaScript"> <!-- function showpay() { if ((document.calc.loan.value == null || document.calc.loan.value.length == 0) || (document.calc.months.value == null || document.calc.months.value.length == 0) || (document.calc.rate.value == null || document.calc.rate.value.length == 0)) { document.calc.pay.value = "Incomplete data"; } else { var princ = document.calc.loan.value; var term = document.calc.months.value; var intr = document.calc.rate.value / 1200; document.calc.pay.value = princ * intr / (1 - (Math.pow(1/(1 + intr), term))); } // payment = principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months)) } // --> </script> The results of this loan payment calculator are for comparison purposes only. They will be a close approximation of actual loan repayments if available at the terms entered, from a financial institution. This is being provided for you to plan your next loan application. To use, enter values for the Loan Amount, Number of Months for Loan, and the Interest Rate (e.g. 7.25), and click the Calculate button. Clicking the Reset button will clear entered values. <p> <center> <form name=calc method=POST> <table width=60% border=0> <tr><th bgcolor="#aaaaaa" width=50%><font color=blue>Description</font></th> <th bgcolor="#aaaaaa" width=50%><font color=blue>Data Entry</font></th></tr> <tr><td bgcolor="#eeeee">Loan Amount</td><td bgcolor="#aaeeaa" align=right><input type=text name=loan size=10></td></tr> <tr><td bgcolor="#eeeee">Loan Length in Months</td><td bgcolor="#aaeeaa" align=right><input type=text name=months size=10></td></tr> <tr><td bgcolor="#eeeee">Interest Rate</td><td bgcolor="#aaeeaa" align=right><input type=text name=rate size=10></td></tr> <tr><td bgcolor="#eeeee">Monthly Payment</td><td bgcolor="#eeaaaa" align=right><em>Calculated</em> <input type=text name=pay size=10></td></tr> <tr><td bgcolor="#aaeeaa"align=center><input type=button onClick='showpay()' value=Calculate></td><td bgcolor="#eeeeaa" align=center><input type=reset value=Reset></td></tr> </table> </form> <font size=1>Enter only numeric values (no commas), using decimal points where needed.<br> Non-numeric values will cause errors.</font> </center> <p align="center"><font face="arial" size="-2">This free script provided by</font><br> <font face="arial, helvetica" size="-2"><a href="http://javascriptkit.com">JavaScript Kit</a></font></p> I'm trying to write a pi calculator that shows a comparison of the calculated value to the actual value. It works, but the output of the comparison shows only the first two decimal spots, followed by two 9s. Why is it showing two 9s and how can I make it more precise? Code: <head> <title> Pi Calculator </title> <script language="JavaScript"> function calcpi(digitsn) { p = 1; s = 0; for (c = 3; c < digitsn; c += 2) { if (s == 0) { p -= 1/c; s = 1; } else { p += 1/c; s = 0; } } p *= 4; return p; } function hid(input) { //Highlight Incorrect Digits pi = "3.14159265358979323846".split(''); input = input.toString().split(''); result = ""; for (c = 0; c < input.length; c++) { if (input[c] != pi[c]) { //result += "; " + input[c] + "!=" + pi[c]; result += "<span style='background-color:red'>" + input[c] + "</span>"; } else { result += input[c]; } } return result; } </script> </head> <body> Repeat algorithm x many times:<br/> <input type="text" value="10000" id="digitsx"/><br/> <input type="button" onClick="document.getElementById('answer').value=calcpi(parseInt(document.getElementById('digitsx').value)).toString()" value="Calculate"/><br/> Result:<br/> <input type="text" readonly id="answer"/><br/> Correct value:<br/> <input type="text" readonly value="3.14159265358979323846"/><br/> <input type="button" value="Compare" onClick="document.getElementById('comparison').innerHTML = hid('3.1499')"/> <div id="comparison"/> </body> Hello all, How to set decimal point in javascript ? e.g. var num1=23.12345; I want output as 23.12 How can i do this ? Please, guide me. Thanks I am trouble in comparing decimal number. var1 = 0.25 var2 = 0.50 if (var1<var2) { alert("Value is less than minimum") } else { return true; } How can I make it work. Thanks for help in advance I am having an issue with converting my decimal to a percentage... any suggestions? Thanks - it's probably an easy solution, but I am lost below is my code (cost * .06) is where the issue is - Thanks! var cost = prompt("What is the cost of your purchase?", "") document.write("Return Value: "+cost,("<br />")); var salestax = cost * .06 document.write("Return Value: "+salestax,("<br />")); var total = cost + salestax document.write(cost + salestax); Hi, Dreamweaver generated the javascript for an onBlur validation for an input textbox. Is it possible to change this code so that I do not have a range but, instead, specific numbers that must be input by the user? I want to ensure that the users input integers and only the following decimals: .25; .5, and .75. [function MM_validateForm() { //v4.0 if (document.getElementById){ var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments; for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]); if (val) { nm=val.name; if ((val=val.value)!="") { if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n'; } else if (test!='R') { num = parseFloat(val); if (isNaN(val)) errors+='- '+nm+' must contain a number.\n'; if (test.indexOf('inRange') != -1) { p=test.indexOf(':'); min=test.substring(8,p); max=test.substring(p+1); if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n'; } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; } } if (errors) alert('The following error occurred:\n'+errors); document.MM_returnValue = (errors == ''); } } </script>] John Hello: I have this js function: Code: <script type="text/javascript"> function updatesum() { document.PaymentForm.newbalance.value = (document.PaymentForm.balance.value -0) - (document.PaymentForm.paymentamount.value -0); } </script> I want to round the value of Code: document.PaymentForm.newbalance.value to two decimal points. May I request some assistance! function below will output price value in text field based on drop-menu option. It's working but I want it's always show value in 2 decimal point for variable 'price'. Can anyone help me with this problem? function findPrice() { if (document.getElementById("region").value == "blank") { var price = document.getElementById("price"); price.value = "<?php echo 'Choose your region to get the price'; ?>"; } if (document.getElementById("region").value == "Peninsular Malaysia") { var price = document.getElementById("price"); price.value= '<?php echo $PM_Price; ?>'; } if (document.getElementById("region").value == "Sabah/Labuan") { var price = document.getElementById("price"); price.value = '<?php echo $SL_Price; ?>'; } if (document.getElementById("region").value == "Sarawak") { var price = document.getElementById("price"); price.value = '<?php echo $SR_Price; ?>'; } } I wrote this microday clock some time ago (with some difficulty) as a project, and now I'm attemtping to make an octal version. I'm still learning JavaScript, so I'm not sure exactly which line/s to edit and what script to add to make it fuctional. if anyone here can show me the most concise script needed to convert the output of this JavaClock from decimal to octal it would be very much appreciated the original script to be converted is shown below : ------------------------------------------------------------ Code: <HTML> <HEAD> <META NAME="JAVACLOCK"> <META HTTP=EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="microday clock" INDEX="all"> <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"> <STYLE> BODY { font-family: Digital Readout Expanded; } </STYLE> <TITLE>microday clock</TITLE> </HEAD> <BODY BGCOLOR=BLACK TEXT=BLUE ALIGN=CENTER LINK=ROYALBLUE VLINK=VIOLET> <DIV ALIGN=CENTER> <span id="clock"> <SCRIPT LANGUAGE="JavaScript"> <!-- Begin function Clock() { Now = new Date(); var DecTime = Now.getTime()/86400000 + 1004472; DecTime = '<FONT SIZE=5><FONT COLOR="#3399FF">' + DecTime.toString().substring(1,4) + ' ' + DecTime.toString().substring(4,7) + '</FONT> <FONT COLOR="#33CC00">' + DecTime.toString().substring(8,11) + ' ' + DecTime.toString().substring(11,14) + '</FONT>' if (document.all) document.all.clock.innerHTML=DecTime else if (document.getElementById) document.getElementById("clock").innerHTML=DecTime else document.write(DecTime) } if (!document.all&&!document.getElementById) Clock() function Increment() { if (document.all||document.getElementById) setInterval("Clock()",86) } window.onload=Increment // End --> </script> </span> I am trying to develop a form that can give an estimate to viewers based on their input and I'm having a problem with decimal points. I couldn't find a very good free script online so I've done some work with this one http://www.mikeandzachsbbq.com/Catering/catering.htm When you enter 10 in the input box, it calculate 10*19.99, giving 199.9, but I want it to show 199.90. I've tried adding toFixed(2) in there, but I'm not able to get that to work....any ideas? Thanks in advance Hey everyone. I am looking to enforce the use of 3 decimal places in an input. I have found some scripts that do this, however, I also want to format it this way in case someone enters LESS than 3 decimal places. If a user enters 1.25, I want it formatted as 1.250 If a user enters 1 I want it formatted as 1.000 etc. Possible? Not sure where to start. I'm a bit confused as I'm just learning PHP/Java, and need to display a percentage. I successfully can display a percentage, however, the decimal like any calculate displays .00000012130 whatever sometimes. How can I round the output to the nearest 10s place? <script language="javascript" type="text/javascript"> var a = "14"; var b = "25"; document.write(parseInt(a) / parseInt(b) * 100); document.write("%"); </script> This is the code I use, however, it needs to be modified so that it rounds to the nearest 10 instead of display a decimal. Hello, Could someone help me with this problem? I'd like the posted result to be a number with two decimals. I know how to make this normally but not with this kind of script. Thanks very much in advance! Code: <html> <head> <script type="text/javascript"> function calcResult(){ document.getElementById('result').innerHTML = ''; var num1 = newNumber(document.getElementById('txt1').value); var num2 = 0.429; var num3 = 1; if(isNaN(num1) || isNaN(num2)){ alert('Please enter a number'); } else { document.getElementById('result').innerHTML = num1 * num2; document.getElementById('entry').innerHTML = num1 * num3; } } window.onload=function(){ document.getElementById('btnCalc').onclick = calcResult; document.getElementById('btnCalc').onclick = calcEntry; } </script> </head> <body> <p> <input type="text" id="txt1" size="5" value="" maxlength="5" /> <button id="btnCalc">Calculate</button> </p> <p> <span id="entry"></span> × 0.429 = <span id="result"></span> </p> </body> </html> |