5

我有一个简单的文本输入,我只想允许浮点数和整数(注意:玉)

input.form-control(type="text", ng-model='usd', ng-pattern="nums",ng-change='convert_to_btc()', placeholder="USD")

但是它不起作用,我总是可以在输入中插入任何字符(我是否需要做更多才能显示某些内容?例如,如果它不正确,则为红色边框?或者应该只是那些字符甚至无法输入?)模式是一个正则表达式,因此不是一个字符串,所以应该没问题???

这是控制器:

app.controller("AppCtrl", function AppCtrl($scope, $http, $interval ) {
    //lots of other stuff
    $scope.nums = /^\-?\d+((\.|\,)\d+)?$/; //note no string, it's a  regex
}

这是生成的 HTML。这可能是问题吗?生成的 HTML 实际上有一个字符串,而不是正则表达式!?

<input type="text" ng-model="usd" ng-pattern="/^\-?\d+((\.|\,)\d+)?$/" ng-change="convert_to_btc()" placeholder="USD" class="form-control ng-dirty ng-valid-parse ng-touched ng-invalid ng-invalid-pattern">
4

2 回答 2

4

我希望这是你想要做的。

请看下面的链接

http://plnkr.co/edit/BGzLbQHy0ZtHYmom8xA3

<!DOCTYPE html>
<html ng-app="">

<head>
 <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.13/angular.js" data-semver="1.3.13">      
 </script>
  <style>
    .ng-invalid-pattern {
      border:1px solid #f00;
    }
  </style>
</head>

<body>
  <p>Hello</p>

  <form name='myform'>      
    <input type="text" name='ip' ng-model="usd" ng-pattern="/^\-?\d+((\.|\,)\d+)?$/" 
  ng-change="convert_to_btc()" placeholder="USD"/>
    <p ng-show='myform.ip.$invalid'>Error</p>
  </form>


  </body>

</html>
于 2015-02-20T03:56:58.947 回答
1

如果您试图阻止用户输入字符/字母并且只允许他们在输入中输入数字,则将其更改<input type="text"<input type="number"

这是 Angular Doc 页面的链接,该页面只允许输入数字:input[number]

于 2016-02-24T21:32:47.503 回答