user2503873
user2503873

Reputation: 3

I need a regex for Decimal Number

I need a regular expression that validates a decimal number, but doesn't require any symbol befor number . ie.

.2 (fine)

.12 (fine)

12 (fine)

12.0 (fine)

12.12 (fine)

But

12.123 (not allow)

-12.12(not allow)

+12.12 (not allow)

Upvotes: 0

Views: 67

Answers (3)

Flash Thunder
Flash Thunder

Reputation: 12036

[0 to 9 as many times as it is possible or none] dot [0 to 9 1 or times]

/^([0-9]+)?(\.[0-9]{1,2})?$/

Edit: added starting and ending limiters.

Upvotes: 1

milk3422
milk3422

Reputation: 660

Just to add to this solution there are multiple online REGEX debuggers. One of my favorite is: http://www.debuggex.com/

It will allow you to put in language, REGEX, and sample data.

Upvotes: 0

Desidero
Desidero

Reputation: 321

The original answer requires numbers before the decimal and numbers after it. after the decimal.

Hooray for edits.

([0-9]+)?(\.[0-9]{1,2})?

Upvotes: 1

Related Questions