Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  114] [ 6]  / answers: 1 / hits: 30529  / 13 Years ago, tue, february 14, 2012, 12:00:00

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.


More From » string

 Answers
103

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);

[#87471] Monday, February 13, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gilbertomaximilianod

Total Points: 208
Total Questions: 96
Total Answers: 111

Location: Northern Mariana Islands
Member since Wed, Feb 24, 2021
3 Years ago
;