1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Remove Formatting from Numbers

Join the Discussion

Questions? Comments?

Often when we allow our visitors to enter numerical data into form fields, they will enter the value including various additional characters to format that number the way they think it ought to be formatted.

Suppose that we allow our visitors to enter currency symbols, thousands separators etc with the numbers that they enter. We can then validate that the symbols that they have entered along with the number are a valid format and reject it if the formatting doesn't make sense. How you validate that depends on exactly what the field is meant to represent.

For the purposes of this article I am going to assume that the validation of what your visitor has input has already been done. Here what we are concerned with is the situation where you want to take the value that has been input and remove all of the formatting so that the value can be saved as a number rather than as text.

For a general number the only characters that we want to keep are the actual digits from 0 through 9, a period (if the number contains decimal places) and a dash (for if the number is negative). Everything else (spaces, commas, currency symbols etc.) need to be removed. This is most easily done using a regular expression that will remove all of the characters that are not those that we want to keep from the text.

num = +num.replace(/[^\d\.-]/g,'');

Where the number can't be negative or contain decimals (eg. a phone number) the removal of the formatting becomes even easier since we can simply remove everything that isn't a number.

phone = +phone.replace(/\D/g,'');

Note that the above codes not only remove all of the unwanted formatting, they also convert the text into a number. If you have additional processing you want to perform on the number as text before converting it into a number you would need to remove the + from the statement (which is what does the text to number conversion for us).

See More About

©2015 About.com. All rights reserved.