ak77
ak77

Reputation: 65

Validate digits between one dot

Trying to get an expression that will validate all these numbers as true "1.1", "11.11", "111111.11", "33.1", "333.11"

Basically any integer before and after one dot.

it should fail for these

"1.", ".1", "1.a", "a.a", "a.1", "1111.2323d111", "1111.11111.1111"

I have this expression "^[0-9]{1,2}([.]{1}[0-9]{1,2})?$

but it failing to detect anything more than 2 digits before and after the dot so i changed it to "^[0-9]([.]{1}[0-9])?$ now its validating .1 and 1. too.

Need some combination of both. please help

Upvotes: 0

Views: 58

Answers (2)

Jerry
Jerry

Reputation: 71538

You used the wrong quantifier at the wrong place.

In:

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

{1,2} means 1 or 2 or the previous character/group/class. If you want to match at least one, then change it to +:

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

And the {1} is redundant, you can remove it.

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

Upvotes: 1

Kendall Frey
Kendall Frey

Reputation: 44326

^\d+\.\d+$

This should do the trick.

Upvotes: 5

Related Questions