Created
January 23, 2017 15:39
-
-
Save LuisEGR/803c68e95e5121d753715a76906de61b to your computer and use it in GitHub Desktop.
Only integer numbers for inputs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('app', []); | |
app.directive('numbersOnly', function () { | |
return { | |
require: 'ngModel', | |
link: function (scope, element, attr, ngModelCtrl) { | |
function fromUser(text) { | |
if (text) { | |
var transformedInput = text.replace(/[^0-9]/g, ''); | |
if (transformedInput !== text) { | |
ngModelCtrl.$setViewValue(transformedInput); | |
ngModelCtrl.$render(); | |
} | |
return transformedInput; | |
} | |
return undefined; | |
} | |
ngModelCtrl.$parsers.push(fromUser); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment