Created
September 7, 2015 20:01
-
-
Save Kyle-Muir/065c8377799616d77ec6 to your computer and use it in GitHub Desktop.
Creates an angular 1.x directive to remove non-numerical input from input fields
This file contains 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
directives.directive('intergenNumbersOnly', () => { | |
'use strict'; | |
return { | |
require: 'ngModel', | |
link: (scope, element, attrs, modelCtrl) => { | |
modelCtrl.$parsers.push(inputValue => { | |
if (inputValue === undefined) { | |
return ''; | |
} | |
var transformedInput = inputValue.replace(/[^0-9]/g, ''); | |
if (transformedInput !== inputValue) { | |
modelCtrl.$setViewValue(transformedInput); | |
modelCtrl.$render(); | |
} | |
return transformedInput; | |
}); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment