Skip to content

Instantly share code, notes, and snippets.

@clamstew
Forked from dkarter/forcelowercase.js
Last active October 5, 2016 02:31
Show Gist options
  • Save clamstew/8fc993b3f5b3c837b9a07225425912a5 to your computer and use it in GitHub Desktop.
Save clamstew/8fc993b3f5b3c837b9a07225425912a5 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', [
'$parse',
function(
$parse
) {
return {
require: 'ngModel',
link: function(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