0

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

ak77
  • 55
  • 1
  • 3
  • 8
  • 1
    I think you should read on the net how to use the ., + and * characters. [0-9]* will mean "any numbers", [0-9]+ will mean "any numbers, at least one" – skoll May 06 '14 at 16:28
  • possible duplicate of [Decimal number regular expression](http://stackoverflow.com/questions/12117024/decimal-number-regular-expression) – Russell Zahniser May 06 '14 at 16:29

2 Answers2

5
^\d+\.\d+$

This should do the trick.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
  • 1
    Not sure, but it looks like OP may want the dot and second set of digits to be option. It would then be `^\d+(\.\d+)?$`, hard to tell from the question though: +1. – Sam May 06 '14 at 16:32
1

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})?$
Jerry
  • 70,495
  • 13
  • 100
  • 144
  • Of course, replace the second `{1,2}` with `+` to allow for more digits after the period. – Jerry May 06 '14 at 16:32