2017-04-03 76 views
0

我有一個輸入文本不是必需的,我想只使用數字字符的ng模式,但是當我提交表單時,輸入無效。爲什麼?ng模式只在不需要的輸入數字

<input type="text" ng-model="price" format="currency" ng-pattern="/^[0-9]*$/"> 

中的jsfiddle,插入產品沒有進入一個價格,並提交:

http://jsfiddle.net/2f6qscrp/266/

PS:我cannont改變輸入的類型!

+0

你的意思是你輸入一個數字,並提交,並出現錯誤? –

+0

不,我有一個有5個輸入的表單,只需要一個(ng-model =「productCode」),但是當我輸入的價格輸入有「ng-invalid-pattern」類時,我無法繼續。 – 3vi

+0

由於正則表達式模式很好,所以問題與代碼有關。如果您需要幫助,請張貼它(任何鏈接到小提琴也是受歡迎的)。 –

回答

0

嘗試/^[0-9]+$/ +符號用於接受一個或多個數字。

0

看看這是否有幫助。

var app = angular.module('demo', []); 
 

 
app.controller('MainCtrl', function($scope) { 
 

 
    $scope.price = ''; 
 
    $scope.seeResults = function() { 
 
    console.log($scope.price); 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.20/angular.min.js"></script> 
 
<!DOCTYPE html> 
 
<html ng-app="demo"> 
 

 
    <head> 
 
    <meta charset="utf-8" /> 
 
    </head> 
 

 
    <body ng-controller="MainCtrl"> 
 
    <form name="form" ng-submit="seeResults()" novalidate role="form"> 
 
     Price <span ng-show="form.$submitted && form.price.$error.pattern" style="color: red;">should be an integer</span> 
 
      <input name="price" type="text" ng-model="price" format="currency" ng-pattern="/^[0-9]*$/">  
 
     <button type="submit" >Submit</button> 
 
    </form> 
 
    </body> 
 
</html>

+0

看到我添加的jsfiddle – 3vi

相關問題