2016-01-20 38 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

相關問題