2016-01-20 36 views
0

我需要一个帮助。我有一个输入字段验证,并且此字段只接受包含使用Angular.js模式的十进制值的数字。我在下面解释我的代码。不能允许使用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> 

这里我的问题是一样的值小数也不会采取这种field.Here我只需要的字符也是不allowed.Please帮助我。

+0

有你尝试过输入type =“number”? –

回答

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();"> 
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" 
+0

它也采取charcter..Please检查这一点。 – satya

+0

@satya我已经更新了正则表达式,试试这个。您可以在http://www.regextester.com/上验证此正则表达式。如果仍然无法解决,请告诉我。 – rroxysam

相关问题