2017-04-18 44 views
0

我已經創建了一個頁面,其中我得到了一個類型數字的輸入,以及我已經使用驗證來檢查什麼時候沒有輸入以及不允許對於特殊字符或除數字以外的任何其他字符。當我們使用像5,10,33534這樣的數字時它工作正常,但是當我們使用數字5.5,10.75時,彈出驗證任何人都可以告訴我如何接受數字和浮點值驗證。 我已經嘗試在html輸入中使用步驟,但它顯示驗證消息,當我使用浮點數。如何允許html輸入中的數字和小數

HTML:

<label class="item item-input InputFormFull"> 
       <span class="input-label"> 
        {{'cost_message' | translate}} 
       </span> 
       <input type="number" placeholder="{{'cost_message' | translate}}" step=".01" 
        ng-model="vm.trade.cost" ng-change="vm.fillStarted()"> 
       </label> 

的Javascript:

if (!isValid(d.field)) {errors.push(d.name + $translate.instant('{{"vempty_message" | translate}}'));} 
     // if (isNaN(d.field)) {errors.push('No special characters allowed in ' + d.name);} 
     if (isNaN(d.field)) {errors.push($translate.instant('{{"vspecalchar_message" | translate}}'));} 
     if (isValid(d.field)) { 
      d.field = d.field.toString(); 
      if (format.test(d.field)) {errors.push('Special characters not allowed');} 
     } 

回答

0

在這種情況下一個更好的辦法是使用正則表達式。

var valid = /^([\d]+(\.)?[\d])?([\d])?$/.test(d.field); 

這裏的表達式會檢查你想要的驗證並相應地返回一個布爾值。

你會發現很多在線工具可以用正則表達式來玩。

相關問題