Abhishek Madhani Abhishek Madhani - 3 years ago 97
Javascript Question

Allow only a single point in decimal numbers

How can I modify this regular expression to allow numbers with just one point?

/[^0-9\.]/g


It currently allows:


  • 0

  • 0.13

  • 0.13.1 (this should not be allowable)


Answer Source

Your regex doesn't matches what you say it matches. You have used negation in character class, and that too without any quantifier. Currently it would match any non-digit character other than ..

For your requirement, you can use this regex:

/^\d+(\.\d+)?$/
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download