I have a javascript function that accepts a number and performs a mathematical operation on the number. However, the number I'm passing in could have a comma in it, and from my limited experience with Javascript I am having problems working with that value. It doesn't seem to treat that as a numeric type.
What's the easiest way to take a parameter with a value of 1,000 and convert it to a numeric 1000?
Use Intl. NumberFormat() to Format Number With Commas in JavaScript.
JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl. NumberFormat() method to format the number with currency.
You can set up your textbox to have an onblur() function so when the user attempts to leave the textbox, you then remove the commas from the value by using the javascript replace function
example:
function checkNumeric(objName) { var lstLetters = objName; var lstReplace = lstLetters.replace(/\,/g,''); }
With input tag here:
<input type="text" onblur="checkNumeric(this);" name="nocomma" size="10" maxlength="10"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With