152

I have a text field that needs to remain only text or decimal. Here is the code that I'm currently using to replace everything except numbers and a decimal point. Issue is, I can't figure out a regex that will identify everything else

document.getElementById(target).value = newVal.replace(/\D[^\.]/g, "");

The \D works fine, but I've tried (?!.), (?!\.), [^.], [^\.] and so on...

Any suggestions for a regular expression that identifies positively with anything except a number or a decimal?

Thanks for the help

guildsbounty
  • 3,326
  • 3
  • 22
  • 34
  • in case you're looking to check for actual numbers rather than numeric digits: http://stackoverflow.com/questions/42328875/javascript-regex-to-remove-all-numbers-with-specific-lenght-or-do-a-persistent – cregox Feb 19 '17 at 15:15

7 Answers7

314

Use this:

document.getElementById(target).value = newVal.replace(/[^0-9.]/g, '');
John
  • 1
  • 13
  • 98
  • 177
Chandu
  • 81,493
  • 19
  • 133
  • 134
  • 1
    @Hugo this does not allow anything, you should just not pass a string that could potentially contain more decimal points and pre-process it first. It's like saying string allows every character... – jave.web Dec 03 '16 at 14:20
  • 4
    Then run the returned value through parseFloat. That will remove additional decimal points. e.g. `parseFloat("46554.4655465.54654.545345.5") = 46554.4655465` – Simon Dec 06 '16 at 03:57
  • 1
    This is great to convert already-formatted numbers like money to a computable float. – lu1s Jan 12 '17 at 00:15
  • 2
    with `replace(/[^0-9.\-]/g, '')` you will get negative numbers too – Mohsen Jan 25 '21 at 04:31
  • Moshen, `replace(/[^0-9.\-]/g, '')` is a good start, but allows subtraction formulas, such as -7-8, which will break data systems without server side checking. The regex must only match the first character for negation. I will not post the solution on SO because the last regex question and answer I posted was deleted by Sammitch, who, like the new owners of SO, don't understand how SO works (posting questions and answers together), and don't know the definition of the word blog (it's web-log). So I don't cast my pearls before swine: let the folks who use SO break their data systems. –  Jun 21 '21 at 16:35
8

Try this:

document.getElementById(target).value = newVal.replace(/^\d+(\.\d{0,2})?$/, "");
linktoahref
  • 7,812
  • 3
  • 29
  • 51
Beachhouse
  • 4,972
  • 3
  • 25
  • 39
8

Removing only decimal part can be done as follows:

number.replace(/(\.\d+)+/,'');

This would convert 13.6667px into 13px (leaving units px untouched).

lubosdz
  • 4,210
  • 2
  • 29
  • 43
3

Regular expression syntax cheatsheet

Go MDN

const str1 = 'hello 123 !@#$%^WORLD?.456_';
const str2 = '৳ $ 20,500.00';

console.log(str1.replace(/[^\d.]/g, "")); //123.456
console.log(str2.replace(/[^\d.]/g, "")); //20500.00
Ovi
  • 179
  • 1
  • 4
2

Check the link Regular Expression Demo

use the below reg exp

[a-z] + [^0-9\s.]+|\.(?!\d)
linktoahref
  • 7,812
  • 3
  • 29
  • 51
divya
  • 21
  • 1
1

Using replace and search to force a decimal number

(text: string )=>{         
     const index = text.search(/(\.)/);                                              
     if (index != text.length -1) {
        let str = text.substring(0, index+1);
        let end = text.substring(index).replace(/[^0-9]/g, '');
        text = str + end;
     }
     return text;
}
1

If you want to keep comma ',' but be converted as '.' then your users will not have problem using both, then use this:

event.target.value.replace(/[^0-9.,]/g, '').replace(',', '.')