-
-
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
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
'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