markduce Posted March 25, 2012 Share Posted March 25, 2012 Hello, I'm a PHP coder, but my client is getting me out of my comfort zone by doing a bit of javascript. I've got basic functionality set up, but I'm just coming across one issue. The thing they want is a simple percentage calculator, where someone enters a value, a percentage and the site then performs calculations based on these values. It's all working fine, except when you actually physically write a percentage sign. I'm looking for some way of stripping out percentage signs. I tried the replace() function, but that just stopped the calculator working. Any advice appreciated, here's my code: function calculate() { a=Number(document.getElementById("edit-gpcalculatorcost").value); b=Number(document.getElementById("edit-gpcalculatorpercentage").value); b3=b; b2=b3/100; c=a/(1-b2); c2=Math.round(c*100)/100; c3=c2*1.2; c4=Math.round(c3*100)/100; document.getElementById("edit-gpcalculatortotal").value=c4; } Quote Link to comment https://forums.phpfreaks.com/topic/259695-really-simple-calculator-problems/ Share on other sites More sharing options...
requinix Posted March 25, 2012 Share Posted March 25, 2012 You can grab the first thing that looks like a number using value.match(/\d+(\.\d+)?/)[0] Quote Link to comment https://forums.phpfreaks.com/topic/259695-really-simple-calculator-problems/#findComment-1331042 Share on other sites More sharing options...
haku Posted March 26, 2012 Share Posted March 26, 2012 a=Number(document.getElementById("edit-gpcalculatorcost").value.replace(/[^0-9]/g, "")); Quote Link to comment https://forums.phpfreaks.com/topic/259695-really-simple-calculator-problems/#findComment-1331094 Share on other sites More sharing options...
markduce Posted March 27, 2012 Author Share Posted March 27, 2012 Thanks haku, that worked a treat. Quote Link to comment https://forums.phpfreaks.com/topic/259695-really-simple-calculator-problems/#findComment-1331658 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.