ng 模式仅数字在不需要输入



>我有一个不需要的输入文本,我想只对数字字符使用 ng 模式,但是当我提交表单时,输入无效。为什么?

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

在 JSFIDDLE 中,插入产品而不输入价格并提交:

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

ps:我无法更改输入类型!

尝试使用/^[0-9]+$/+符号表示接受一个或多个数字。

看看这是否有帮助。

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>

相关内容

最新更新