31

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.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
MrGlass
  • 9,094
  • 17
  • 64
  • 89
  • 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. – hugomg Feb 14 '12 at 14:00
  • 1
    Perhaps. Right now, I'm only in the US. And there should be only integers. – MrGlass Feb 14 '12 at 14:02

4 Answers4

62

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);
adelphus
  • 10,116
  • 5
  • 36
  • 46
  • add ; to the end of your lines, and its actually parseInt(str). Otherwise, works great, thanks. – MrGlass Feb 14 '12 at 13:57
  • 1
    The semicolons are actually optional and I was following your example of parseint()! I concede to both your points though and will edit :-) – adelphus Feb 14 '12 at 14:01
  • 2
    The only thing to be wary of here is for locality differences of decimal notation; comma vs. period, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString – kalisjoshua Apr 13 '17 at 12:29
5

or even better

var s="jdjsghd0182.99";
var str = parseFloat(s.replace(/[^0-9 | ^.]/g, ''));
James Montagne
  • 77,516
  • 14
  • 110
  • 130
Venkat Reddy
  • 1,046
  • 1
  • 8
  • 13
  • 4
    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. – kalisjoshua Apr 13 '17 at 12:26
  • 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 – Venkat Reddy Apr 13 '17 at 21:14
  • 1
    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. – kalisjoshua Apr 17 '17 at 12:17
  • 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 – twm Sep 04 '18 at 20:46
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);

blankabout
  • 2,597
  • 2
  • 18
  • 29
  • This should be the answer :) – Tim Malone May 24 '16 at 23:04
  • 5
    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 – Andrew Luhring Jun 19 '17 at 18:46
  • The OP asked 'how can I remove the commas and/or force the input to an integer', so it is not wrong. – blankabout Feb 17 '18 at 08:38
  • 1
    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]);` – twm Sep 04 '18 at 17:48
  • 1
    PS - I think this approach will also break for negative integers. – twm Sep 04 '18 at 18:09
-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.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 1
    replace() will only replace the *first* comma. If the number is large enough to have more than one comma, this won't work. – adelphus Feb 14 '12 at 13:54