Sergio Robledo Arango
Sergio Robledo Arango

Reputation: 177

Angular JS directive for number greater than 0

I have the following input <input type="text" name="area" class="form-control" ng-pattern="onlyNumbers" ng-model="myForm.area" required> and the ng-pattern only allows the input to have numbers.

$scope.onlyNumbers = /^\d+$/;

I also would like some way to tell the input that the number must be greater than 0. Meaning that the user must not input something like 023, it must be 23.

Upvotes: 2

Views: 8104

Answers (3)

thorinkor
thorinkor

Reputation: 966

Option allowing floats with , or . separators.

$scope.onlyNumbers = /^(0*[1-9][0-9]*([\.\,][0-9]+)?|0+[\.\,][0-9]*[1-9][0-9]*)$/;

Upvotes: 1

paulhauner
paulhauner

Reputation: 1516

This will allow for any number above zero (eg, 0.0002).

$scope.onlyNumbers = /^0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$/

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76656

You can just change the regex:

$scope.onlyNumbers = /^[1-9][0-9]*$/;

Upvotes: 6

Related Questions