Created
          January 21, 2015 17:25 
        
      - 
      
- 
        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
  
        
  
    
      This file contains hidden or 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
  
            
Initial values don't display until I give focus to the control.