1

Regex to allow only numbers and single dot when entering number to textbox in jquery.

Please suggest any regex to allow only numbers and single dot in textbox.

I have tried the following code.

$("#amountId").val().replace( /[^0-9]+/g, '')
R J.
  • 1,382
  • 9
  • 22
  • 37

4 Answers4

5

[-+]?([0-9]*\.[0-9]+|[0-9]+).

See Matching Floating Point Numbers With Regex

HamZa
  • 14,051
  • 11
  • 52
  • 72
ahPo
  • 364
  • 2
  • 9
1

This will find something like this 1234.5678
/\d+\.\d+/

HamZa
  • 14,051
  • 11
  • 52
  • 72
alex
  • 11
  • 1
0

This regex should do the trick for you, \d+\.$, and here is a Rubular to prove it. You could even consider prefacing the string with the ^ so that nothing can come before the digits too, like this, ^\d+\.$.

Mike Perrenoud
  • 64,877
  • 28
  • 152
  • 226
0

I would try using the following:

\d+\.\d?{2}

http://rubular.com/r/a83BWznDuy

Michael Durrant
  • 89,394
  • 88
  • 305
  • 468