ng-pattern允许十进制比率 [英] ng-pattern that allows decimal ratios
问题描述
我有一个ng-pattern
可以验证4:6之类的定量,如下所示:
I'm having an ng-pattern
that validates for a ration like 4:6 as shown below:
ng-pattern="/^(\d+):(\d+)$/"
我还想验证是否在上述验证中输入了十进制数字作为口粮例如5.4:7.1
我尝试将ng-pattern
设置为
I want also to validate if decimal numbers are entered as rations along with the above validation.Example :5.4:7.1
I trid by giving ng-pattern
as
ng-pattern="/^(\.[0-9]{1,2}+):(\.[0-9]{1,2}+)$/"
但不触发验证.请向我建议我要去哪里了.
But not triggering the validation.Please suggest me where I'm going wrong..
推荐答案
您的^(\.[0-9]{1,2}+):(\.[0-9]{1,2}+)$
在JS中不是有效的模式,因为限制量词{min,max}
不能跟在+
后面(JS不支持所有格量词正则表达式引擎).即使删除加号并使用^(\.[0-9]{1,2}):(\.[0-9]{1,2})$
,该模式也将匹配.1:.3
或.34:.45
之类的字符串,并且不允许3:45
(点是必须的).
Your ^(\.[0-9]{1,2}+):(\.[0-9]{1,2}+)$
is not a valid pattern in JS as the limiting quantifier {min,max}
cannot be followed with +
(no possessive quantifiers are supported by JS regex engine). Even if you remove the pluses and use ^(\.[0-9]{1,2}):(\.[0-9]{1,2})$
, the pattern would match strings like .1:.3
or .34:.45
and would not allow 3:45
(dots would be obligatory).
如果要允许类似.5
的值,请使用以下正则表达式:
Use the following regex if you want to allow values like .5
:
ng-pattern="/^\d*\.?\d+:\d*\.?\d+$/"
请参见 regex演示.
或者,您可以使用
ng-pattern="/^(\d+\.)?\d+:(\d+\.)?\d+$/"
仅允许包含非空整数部分的数字.请参见此regex演示.
to only allow numbers with non-empty integer part. See this regex demo.
这篇关于ng-pattern允许十进制比率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!