Skip to content

Instantly share code, notes, and snippets.

@lelandcope
Created July 3, 2015 00:36
Show Gist options
  • Save lelandcope/50cb1f98d02472f5c0dc to your computer and use it in GitHub Desktop.
Save lelandcope/50cb1f98d02472f5c0dc to your computer and use it in GitHub Desktop.
AngularJS Lowercase Directive that will force the input to be lowercased
_module.directive('lowercase', function () {
return {
require: 'ngModel',
link: function ($scope, elem, attrs, ngModelCtrl) {
var lowercase = function (inputValue) {
if(inputValue == undefined) inputValue = '';
var lowercased = inputValue.toLowerCase();
if(lowercased !== inputValue) {
ngModelCtrl.$setViewValue(lowercased);
ngModelCtrl.$render();
}
return lowercased;
}
ngModelCtrl.$parsers.unshift(lowercase);
lowercase($scope[attrs.ngModel]);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment