2

I want my hours input field to allow only numbers utill 2nd decimal point (numbers with only a single dot).

I used this expression for the same /^[0-9]+(\.[0-9]{1,2})?$/ and this allows 1.1. which should not be allowed

Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54

1 Answers1

2

The pattern is matching the right type of input. The type of the input should be set to text:

<input type="text" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" />

A regex validation check is not supported with type="number".

UPDATE

To only allow float/integer values between 1 and 60, use

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

See the regex demo

  • ^ - start of sting
  • ([1-9]|[1-5][0-9]|60(\.0{1,2})?$) - one of the following:
    • [1-9] - digit from 1 to 9 (no 0 allowed)
    • [1-5][0-9] - numbers from 10 to 59
    • 60(\.0{1,2})?$ - 60 that is optionally followed with .0 or .00 and then an end of string should follow
  • (\.[0-9]{1,2})? - optional (1 or 0) . followed with 1 or 2 any digits before...
  • $ - the end of string
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563