How to prevent typed letters in a input
where only angled numbers should be typed? Only with policies is it possible?
How to prevent typed letters in a input
where only angled numbers should be typed? Only with policies is it possible?
Use a directive that removes all non-numeric characters.
angular.module('myApp', [])
.directive('numerosApenas', function() {
return {
require: 'ngModel',
link: function (scope, element, attr, ngModelCtrl) {
function parserNumerico(textoOriginal) {
// Troque tudo o que não for um dígito válido por uma string vazia
var inputSemNumeros = textoOriginal.replace(/[^0-9]/g, '');
if(inputSemNumeros !== textoOriginal) {
// reverta o valor para a versão sem números.
ngModelCtrl.$setViewValue(inputSemNumeros);
ngModelCtrl.$render();
}
return Number(inputSemNumeros);
}
ngModelCtrl.$parsers.push(parserNumerico);
}
};
})
.controller('myController', function($scope){
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script><divng-app="myApp">
<div ng-controller='myController'>
<input type='text' numeros-apenas ng-model='valor' />
</div>
</div>