Skip to content

Instantly share code, notes, and snippets.

@dkarter
Created January 21, 2015 17:25
Show Gist options
  • Save dkarter/faa1838547f81c9a61a1 to your computer and use it in GitHub Desktop.
Save dkarter/faa1838547f81c9a61a1 to your computer and use it in GitHub Desktop.
Angular directive to force lowercase letters on an input textbox as you type
'use strict';
/**
* @ngdoc directive
* @name myapp.directive:forceLowerCase
* @description
* # forceLowerCase
*/
angular.module('myapp')
.directive('forceLowerCase', function ($parse) {
return {
require: 'ngModel',
link: function postLink(scope, element, attrs, modelCtrl) {
var lowerize = function(inputValue) {
if (!inputValue) { return inputValue; }
var lowerized = inputValue.toLowerCase();
if(lowerized !== inputValue) {
modelCtrl.$setViewValue(lowerized);
modelCtrl.$render();
}
return lowerized;
};
var model = $parse(attrs.ngModel);
modelCtrl.$parsers.push(lowerize);
lowerize(model(scope));
}
};
});
@trevorhreed
Copy link

Initial values don't display until I give focus to the control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment