Ng-pattern that allows decimal relationships

I have ng-pattern

one that checks for a ration like 4: 6 like below:

ng-pattern="/^(\d+):(\d+)$/"

      

I want to also check if decimal numbers will be entered as solders along with the above check. Example: 5.4: 7.1 I trid, giving ng-pattern

as

ng-pattern="/^(\.[0-9]{1,2}+):(\.[0-9]{1,2}+)$/"

      

But don't run the check. Please suggest me where I go wrong.

+2


source to share


1 answer


Yours is ^(\.[0-9]{1,2}+):(\.[0-9]{1,2}+)$

not a valid pattern in JS as the constraint quantifier {min,max}

cannot be executed with +

(no adventive quantifiers are supported by the JJ regex engine). Even if you remove the pros and use ^(\.[0-9]{1,2}):(\.[0-9]{1,2})$

, the pattern will match strings like .1:.3

or .34:.45

and will not allow 3:45

(dots must be required).

Use the following regex if you want to allow values โ€‹โ€‹such as .5

:

ng-pattern="/^\d*\.?\d+:\d*\.?\d+$/"

      

See demo regex .



Alternatively, you can use

ng-pattern="/^(\d+\.)?\d+:(\d+\.)?\d+$/"

      

to resolve numbers with a non-empty integer part. See this regex demo .

+3


source







All Articles