简体   繁体   English

ng-pattern允许十进制比率

[英]ng-pattern that allows decimal ratios

I'm having an ng-pattern that validates for a ration like 4:6 as shown below: 我有一个ng-pattern可以验证4:6之类的定量,如下所示:

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

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 我还想验证十进制数字是否与上述验证一起作为口粮输入。例如:5.4:7.1我尝试通过给ng-pattern作为

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

But not triggering the validation.Please suggest me where I'm going wrong.. 但不要触发验证。请向我建议我要去哪里了。

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). 您的^(\\.[0-9]{1,2}+):(\\.[0-9]{1,2}+)$在JS中不是有效的模式,作为限制量{min,max}不能用+跟随(JS正则表达式引擎不支持所有格量​​词)。 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). 即使删除加号并使用^(\\.[0-9]{1,2}):(\\.[0-9]{1,2})$ ,该模式也将匹配.1:.3字符串.1:.3.34:.45 ,不允许3:45 (点是必填项)。

Use the following regex if you want to allow values like .5 : 如果要允许.5值,请使用以下正则表达式:

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

See the regex demo . 参见regex演示

Alternatively, you may use 或者,您可以使用

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

to only allow numbers with non-empty integer part. 只允许包含非空整数部分的数字。 See this regex demo . 请参阅此正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM