我输入的文字不是必需的,我想只使用ng-pattern作为数字字符,但是当我提交表单时,输入无效。为什么?
<input type="text" ng-model="price" format="currency" ng-pattern="/^[0-9]*$/">
在jsfiddle中,插入产品而不输入价格并提交:
http://jsfiddle.net/2f6qscrp/266/
ps:我不能改变输入的类型!
答案 0 :(得分:0)
尝试使用/^[0-9]+$/
+符号表示接受一个或多个数字。
答案 1 :(得分:0)
看看这是否有帮助。
var app = angular.module('demo', []);
app.controller('MainCtrl', function($scope) {
$scope.price = '';
$scope.seeResults = function() {
console.log($scope.price);
};
});
&#13;
<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>
&#13;