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; } 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] 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, "")); 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. Link to comment https://forums.phpfreaks.com/topic/259695-really-simple-calculator-problems/#findComment-1331658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.