32

I have a page with some elements that are controlled by the user. One of these is a text input field, where the user is supposed to input a number. Everything works well if the user only inputs digits (EG 9000), but is the user uses comma notation (the being 9,000) javascript doesn't take the input as an integer.

How can I remove the commas and/or force the input to an integer? I tried using parseint(), but it doesn't seem to work with commas.

2
  • 2
    Note that in some countries people use commas to separate the decimals and dots to separate the thousands. You might want to consider using a locale-aware library to do the number parsing for you. Commented Feb 14, 2012 at 14:00
  • 1
    Perhaps. Right now, I'm only in the US. And there should be only integers. Commented Feb 14, 2012 at 14:02

4 Answers 4

63

Use a global regular expression to replace all commas with an empty string:

var str = "12,345,678";
str = str.replace(/,/g, "");
parseInt(str, 10);
Sign up to request clarification or add additional context in comments.

3 Comments

add ; to the end of your lines, and its actually parseInt(str). Otherwise, works great, thanks.
The semicolons are actually optional and I was following your example of parseint()! I concede to both your points though and will edit :-)
The only thing to be wary of here is for locality differences of decimal notation; comma vs. period, developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
5

or even better

var s="jdjsghd0182.99";
var str = parseFloat(s.replace(/[^0-9 | ^.]/g, ''));

4 Comments

I disagree that this is "better" at all. This recommendation, as well as @blankabout's, is changing the input from a user and once you do that you would also need to ask the user if your interpretation of their input is what they actually intended. Whereas the commas are an acceptable and expected deviation the addition of other characters is not.
Thanks for your feedback, but commas are not only acceptable characters, people tend to put $,# etc for currencies and also people tend to add spaces. common misconceptions is prices contains only digits and punctuation. (Germans can write 12,- €). another misconception is you separate big prices by grouping numbers in triplets (thousands). (One writes ¥1 0000). pWhen you write code, please make it accessible for every type of user
I think we agree, but the regex above is overly simplistic; it should be looking for specific patters not just any stream of input that happens to contain numbers which could be interpreted as a number. For instance, "3 sets of 4" should not be interpreted as "34" nor should "3 - 4". That is my point but possibly not well worded before and possibly also might be stretching the subject of the original question.
I think this implementation will break for negative integers, & the minus sign could trail too if you want to support your example of "12,- €" (or some POS systems). That example also illustrates kalisjoshua's point - "12,- €" should not be accepted as input to be interpreted as if a German had written it because "12,000 €" would yield 12000 when it should actually yield 12. Throwing away non-numeric characters will also trigger false positive minus signs for units with hyphens. I don't know if there are currencies like that, but the question's not limited to currencies. "ft-lb" would be legit
2

Or even better, given the general unreliability of user input, use this to get rid of all non-numeric characters:

var s = "9,Ljk876";
var t = parseInt(s.replace(/[^0-9]/g, ''));
alert ("s:" + s + ", t:" + t);

5 Comments

This should be the answer :)
This is wrong. If you use that regex, it will replace decimals which would change the number- for example: parseInt('1,200.34'.replace(/[^0-9]/g, '')); would return: 120034
The OP asked 'how can I remove the commas and/or force the input to an integer', so it is not wrong.
It's probably better to reject input such as 1,200.34. As a user, I would freak out if I thought I was transferring $1,200.34 and transferred $120,034.00 instead. Also, I think the expectation is that if a floating point value is accepted as input, you get the floor when converting to an integer. You can do that by tweaking what you wrote to chop off everything after the first dot: parseInt(s.replace(/[^0-9.]/g, '').split('.')[0]);
PS - I think this approach will also break for negative integers.
-1

maybe

 parseint(ny9000withCommas.replace(/\,/g,""))

lets talk about the restriction :

you can/should allow the user to enter both 9000 & 9,000

you can check validy via REGEX.

in the server side - you should eleminate the commas and treat it as integer.

1 Comment

replace() will only replace the first comma. If the number is large enough to have more than one comma, this won't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.