简体   繁体   English

ng-pattern仅数字,不需要输入

[英]ng-pattern only numbers in not required input

I've an input text not required and I want to use a ng-pattern for only digits character, but when I submit the form, the input isn't valid. 我不需要输入文本,我只想对数字字符使用ng-pattern,但是当我提交表单时,输入无效。 Why? 为什么?

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

in jsfiddle, insert product without enter a price and submit: 在jsfiddle中,插入产品而不输入价格并提交:

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

ps: I cannont change the type of input! ps:我无法更改输入类型!

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

See if this helps. 看看是否有帮助。

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM