【发布时间】:2015-06-11 12:09:13
【问题描述】:
我正在使用 angularjs 构建一个表单,其中有一个金额字段。
我想验证金额并设置格式,限制无效金额,只输入有效金额,其余全部丢弃。有效金额为:
1.23
0.99
所以,基本上,1 位数字后跟 2 个小数点。
我对在过滤器或指令之间使用感到困惑,因为我从未使用过它们中的任何一个。如果有人过去做过类似的事情,如果您可以与我分享或者您可以给我解决方案,我将不胜感激。
我使用过类似 ng-pattern="/^[0-9]*\.[0-9][0-9]$/ 的 ng-pattern,但对我不起作用。
我正在使用 AngularJS 1.4 最新版本。
编辑 - 我的 DODE
<input type="number"
name="globalNetPrice"
value="{{netprice.globalNetPrice}}"
ng-model="netprice.globalNetPrice"
class="form-control"
required
ng-minlength="0.01"
ng-maxlength="999"
ng-pattern="/^[0-9]+.[0-9][0-9]$/"
>
<p ng-show="netpriceForm.globalNetPrice.$invalid && !netpriceForm.globalNetPrice.$pristine">
<small class="error" ng-show="netpriceForm.globalNetPrice.$error.required">Net Price is required.</small>
<small class="error" ng-show="netpriceForm.globalNetPrice.$error.number">That is not a Net Price. Please input a valid Net Price.</small>
<small class="error" ng-show="netpriceForm.globalNetPrice.$error.pattern">That is not a valid Net Price. Pattern not valid.</small>
</p>
【问题讨论】:
-
试试这个 ng-pattern="/^[0-9]+.[0-9][0-9]$/" 。请参阅 plnkr 链接plnkr.co/edit/CbNzrl?p=preview
-
@Hurix 我就是这样做的,但我可以在小数点后输入这么多数字。我在上面粘贴代码,请检查有什么问题
标签: angularjs angularjs-directive angularjs-filter