2017-09-25 196 views
0

我使用的是AngularJS和Kendo UI。我只想在我的输入组件中打印整数。我试过使用正则表达式,但它并没有帮助我。只有整数允许在输入中打印

输入这是我的配置:

input(
    ng-pattern='vm.regexp' 
    min='1' 
    ng-model='vm.maxContentCounter' 
) 
vm.regexp = /^(0|[1-9]\d*)$/; 

该输入允许打印逗号和圆点,而我可以打印值小于1。我如何允许只打印整数值?

回答

1

<html ng-app="myApp"> 
 
<head> 
 
\t <title></title> 
 
</head> 
 
<body ng-controller="myCtrl"> 
 
<input type="text" valid-decimal-number ng-model="Amount" ng-blur="ConvertToDecimal()"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script type="text/javascript"> 
 
var app = angular.module("myApp", []); 
 
app.controller('myCtrl', ['$scope', function ($scope) { 
 
$scope.ConvertToDecimal = function (val) { 
 
\t if(isNaN($scope.Amount)){ 
 
\t \t alert("given input is not a number"); 
 
\t \t return; 
 
\t } 
 
     var num = Number($scope.Amount); 
 
     var dupNum=Math.floor(num); 
 
     if(num !=dupNum){ 
 
     alert("given input is not a number"); 
 
\t \t return; 
 
     } 
 
     $scope.Amount=num; 
 
    } 
 
}]); 
 
</script> 
 
</body> 
 
</html>

+0

输入接受'1.2' ... – DontVoteMeDown

+0

你的意思是不应该接受1.2? –

+0

*“只有整数”*读取标题... – DontVoteMeDown