最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular JS directive for number greater than 0 - Stack Overflow

programmeradmin4浏览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.

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.

Share Improve this question edited Jul 4, 2014 at 17:03 Beed 4603 silver badges10 bronze badges asked Jul 4, 2014 at 16:47 Sergio Robledo ArangoSergio Robledo Arango 1771 gold badge5 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You can just change the regex:

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

Option allowing floats with , or . separators.

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

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]*$/

发布评论

评论列表(0)

  1. 暂无评论