使用Angular.js

时间:2016-01-20 07:57:10

标签: javascript angularjs ng-pattern

我需要一个帮助。我有一个输入字段验证,这个字段只接受包含十进制值的数字也使用Angular.js模式。我在下面解释我的代码。

<div class="input-group bmargindiv1 col-md-12">
    <span class="input-group-addon ndrftextwidth text-right" style="width:180px">
          Unit Sale Price :
    </span>
    <div ng-class="{ 'myError': billdata.usp.$touched && billdata.usp.$invalid }">
        <input type="text" name="usp" id="usp" class="form-control"
                placeholder="Add Unit Sale Price" 
                ng-model="unit_sale_price" 
                ng-keypress="clearField('usp');" 
                ng-keyup="setLatestSalePrice();" 
                ng-pattern="/^(0|[1-9][0-9]*)$/">
        <div style="clear:both;"></div>
    </div>
</div>
<div class="help-block" ng-messages="billdata.usp.$error"
     ng-if="billdata.usp.$touched || billdata.usp.$error.pattern">
    <p ng-message="pattern" style="color:#F00;">
       This field needs only number(e.g-0,1..9).
     </p>
</div>

这里我的问题是这个字段也没有采用十进制的值。这里我只需要字符也是不允许的。请帮帮我。

2 个答案:

答案 0 :(得分:0)

您可以使用input[type="number"]解决此问题:

<input type="number" name="usp" id="usp" class="form-control" placeholder="Add Unit Sale Price" ng-model="unit_sale_price" ng-keypress="clearField('usp');" ng-keyup="setLatestSalePrice();">

答案 1 :(得分:0)

使用也可以使用正则表达式:

<input type="text" name="usp" id="usp" class="form-control" placeholder="Add Unit Sale Price" ng-model="unit_sale_price" ng-keypress="clearField('usp');" ng-keyup="setLatestSalePrice();" ng-pattern = "^[0-9]+([.][0-9]+)?$">

并使用以下表达式进行错误

 ng-show="regForm.password.$error.pattern"
相关问题