-1

I have an html field that need to restrict the user input to either type number from 1 to 9 with a precision of two numbers, or just the number 10, so that the number 10 could not have a precision. for exp : 5.50 ,7 , 6.45 , 10 // tolerated numbers 10.5 , 20, 15 //not tolerated.

Thanks in advance.

krachleur
  • 356
  • 3
  • 14

3 Answers3

5

You could do it like this:

^([1-9](?:\.\d\d)?|10)$

Explanation

  • From the beginning of the string ^
  • Match a digit between 1 and 9 [1-9]
  • A optional non capturing group which matches a dot and 2 digits (?:\.\d\d)?
  • or match 10
  • Assert the end of the string $
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

Try this

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

Deepak Kumar T P
  • 1,076
  • 10
  • 20
  • 1
    Are you sure this matches the OP's requirements? [https://regex101.com/r/ENfCQa/1](https://regex101.com/r/ENfCQa/2) "need to restrict the user input to either type number from 1 to 9 .." – The fourth bird Dec 28 '17 at 11:10
  • 1
    Doesn't this still match a leading zero? [https://regex101.com/r/RoRxbE/1](https://regex101.com/r/RoRxbE/1) – The fourth bird Dec 28 '17 at 11:18
  • changed the 0 in the first [0-9] to 1, gives [1-9] and voilaaa – krachleur Dec 28 '17 at 11:21
  • 1
    Yes, but "with a precision of two numbers". Your regex still matches `6.4` or `6.` [https://regex101.com/r/7dCY5r/1/](https://regex101.com/r/7dCY5r/1/) – The fourth bird Dec 28 '17 at 11:28
1

What about this ?

^([1-9]{1}(\.\d{1,2})?|10)$

Gautier
  • 1,066
  • 1
  • 13
  • 24