stonypaul
stonypaul

Reputation: 677

Regex for a positive decimal or an integer string up to 7 characters in length

I'm trying to write a regex for a RegularExpressionValidator Control that will allow a decimal or integer with the following conditions:

A solitary zero is allowed

So these are good....

0
0.1
0.12
1.34
12.45
123.67
1234.67
12345.7

and these are bad.....

-0
-0.1
012.4
123.560
123...7

could someone advise on this please. i've had a few attempts and the main component i'm struggling with is checking for just one decimal point. thank you

Upvotes: 2

Views: 1364

Answers (2)

Bohemian
Bohemian

Reputation: 425043

Here's another way of doing it, without look-behinds:

^(?=.{1,7}$)(0(?=\.|$)|[1-9])\d*(\.\d?[1-9])?$

See a live demo on rubular

Upvotes: 0

anubhava
anubhava

Reputation: 785196

Following regex should work for you:

(?!^0[1-9])(?=^([0-9])+(\.\d{1,2}(?<!0))?$)^.{1,7}$

Live Demo: http://www.rubular.com/r/Y3wVkKST1I

Upvotes: 2

Related Questions