Skip to content

Instantly share code, notes, and snippets.

@ronaiza-cardoso
Forked from dkarter/forcelowercase.js
Created January 3, 2017 12:00
Show Gist options
  • Save ronaiza-cardoso/788ae70354733f0b19b1c1d2274b74d2 to your computer and use it in GitHub Desktop.
Save ronaiza-cardoso/788ae70354733f0b19b1c1d2274b74d2 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));
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment