1

Angular highlights the text box with red if a non-integer number is used.

(function()
 {
     var app = angular.module('example', []);

     app.controller('InputController', function()
      {
   this.number = '';
      }
     );
 })();
<html ng-app="example">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form ng-controller="InputController as ctl" ng-submit="inputForm.$valid">
  <input type="number" step="0.001" pattern="(-|\+)?\d*\.?\d+" ng-model="ctl.number">
  <input type="submit">
</form>
</html>

If I set the step to, for example, 0.001, then it will highlight red when I put anything with more than three decimal places, e.g. 9.1234.

If I put the pattern (-|\+)?\d*\.?\d+, that changes nothing.

Combining step with pattern does not fix my problem.

How can I get it to accept anything that's a number, not just integers, and only numbers?

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90

1 Answers1

1

You have two options available either set the step size, this will up to n decimal places to be valid.

<input type="number" ng-model="ctl.number" step="0.01">

or do as Tushar suggested and validate it yourself with ng-pattern

Community
  • 1
  • 1
Ant Kennedy
  • 1,230
  • 7
  • 13
  • Thanks for taking the time to answer. Adding a step doesn't allow any number, for example, your step doesn't allow 0.0001. ng-pattern (or pattern) doesn't stop the input box from turning red. – Matt Ellen May 25 '16 at 16:08
  • for a quick fix you can just make the step size really tiny `step="0.00000000000000001"` but that isn't the correct answer long term – Ant Kennedy May 25 '16 at 16:13