Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbers with commas in Javascript

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?

like image 615
Mike Cole Avatar asked May 07 '09 03:05

Mike Cole


People also ask

How do you put commas in numbers in HTML?

Use Intl. NumberFormat() to Format Number With Commas in JavaScript.

How do you format numbers 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.


1 Answers

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"/> 
like image 64
TStamper Avatar answered Sep 18 '22 10:09

TStamper