26

What would be the regex to allow digits and a dot? Regarding this \D only allows digits, but it doesn't allow a dot, I need it to allow digits and one dot this is refer as a float value I need to be valid when doing a keyup function in jQuery, but all I need is the regex that only allows what I need it to allow.

This will be in the native of JavaScript replace function to remove non-digits and other symbols (except a dot).

Cheers.

MacMac
  • 34,294
  • 55
  • 151
  • 222

5 Answers5

55

If you want to allow 1 and 1.2:

(?<=^| )\d+(\.\d+)?(?=$| )

If you want to allow 1, 1.2 and .1:

(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$| )

If you want to only allow 1.2 (only floats):

(?<=^| )\d+\.\d+(?=$| )

\d allows digits (while \D allows anything but digits).

(?<=^| ) checks that the number is preceded by either a space or the beginning of the string. (?=$| ) makes sure the string is followed by a space or the end of the string. This makes sure the number isn't part of another number or in the middle of words or anything.

Edit: added more options, improved the regexes by adding lookahead- and behinds for making sure the numbers are standalone (i.e. aren't in the middle of words or other numbers.

Håvard
  • 9,900
  • 1
  • 41
  • 46
  • That regular expression will report a match on pretty much anything. For example, in .NET the result of `Regex.Replace("xyzzy", @"\d*(\.\d+)?", "!");` will be `!x!y!z!z!y!`. I'm pretty sure the results in JavaScript will be the same. Since everything's optional, the regex will match at every position. – Jim Mischel Apr 06 '11 at 19:27
  • @Jim Mischel, good call. I edited the answer with improved regexes. – Håvard Apr 06 '11 at 19:41
  • 5
    @[Harpyon] Is there something missing? I get the message "Invalid group": /(?<=^| )\d+(\.\d+)?(?=$| )|(?<=^| )\.\d+(?=$|)/ – Zesty May 18 '13 at 13:10
  • 2
    Lookbehinds aren't supported by JavaScript apparently. – Chrillewoodz Mar 27 '16 at 17:20
25
\d*\.\d*

Explanation:

\d* - any number of digits

\. - a dot

\d* - more digits.

This will match 123.456, .123, 123., but not 123

If you want the dot to be optional, in most languages (don't know about jquery) you can use

\d*\.?\d*
Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
  • 1
    `\D` is any character **except** digits. `\d` is what you mean. – Francisc Apr 06 '11 at 18:04
  • 2
    `\D` is the inverse of "match a digit"-- it will match anything but a digit! – Platinum Azure Apr 06 '11 at 18:04
  • 1
    Thank you, @Francisc and @Platinum. I fixed it now. The reason for the mistake is that I copied the `\D` from the original question. I know in Python `\d` means "digit" and `\D` means "anything but a digit", but I thought that if the OP mentioned `\D` then it must be different in jQuery. According to your comments, apparently my first thought was correct and I should have stuck to it :) – Ilya Kogan Apr 07 '11 at 03:14
  • Getting invalid escape sequence in C#. Any help plz – Tariq Mar 31 '16 at 07:15
7

Try this

boxValue = boxValue.replace(/[^0-9\.]/g,"");

This Regular Expression will allow only digits and dots in the value of text box.

Narasimha
  • 105
  • 1
  • 3
3

My try is combined solution.

string = string.replace(',', '.').replace(/[^\d\.]/g, "").replace(/\./, "x").replace(/\./g, "").replace(/x/, ".");
string = Math.round( parseFloat(string) * 100) / 100;

First line solution from here: regex replacing multiple periods in floating number . It replaces comma "," with dot "." ; Replaces first comma with x; Removes all dots and replaces x back to dot.

Second line cleans numbers after dot.

Community
  • 1
  • 1
Roman Losev
  • 1,911
  • 19
  • 26
  • 1
    Thanks. After searching since 2 Hours, I got this answer. And, this is fitting perfectly in my set of requirements (only numeric digits along with one dot). Thanks Once Again. – Nana Partykar Feb 09 '17 at 12:29
0

Try the following expression

/^\d{0,2}(\.\d{1,2})?$/.test()
Sumit Kumar
  • 763
  • 9
  • 14