0

I have a form with input fields. One input field allows alphanumeric values. But if the value contains letters, then the letters have to be uppercase. How can I implement this approach? Is it possible to define this in the input field, when the user does an input then the letters are automatically shown in uppercase?

The View:

<div class="form-group-sm has-feedback">
  <label class="control-label" for="article-id">Article Nr.</label>
  <input type="text" 
         class="form-control" 
         name="Article" 
         id="article-id"
         ng-model="selected.article" 
         ng-pattern="/^[a-zA-Z]{2}[a-zA-Z\d]{9}[0-9]$/"
         ng-required="true"
         />
</div>
...
//the other inputs are the same definition

It is important for me to save the value in the DB with uppercase letters.

yuro
  • 2,189
  • 6
  • 40
  • 76

2 Answers2

2

Just create a simple directive ,this answer is based on the answer here: Angular.js: How to autocapitalize an input field?.

myApp.directive('capitalize', function() {
   return {
     require: 'ngModel',
     link: function(scope, element, attrs, modelCtrl) {
        var capitalize = function(inputValue) {
           if(inputValue == undefined) inputValue = '';
           var capitalized = inputValue.toUpperCase();
           if(capitalized !== inputValue) {
              modelCtrl.$setViewValue(capitalized);
              modelCtrl.$render();
            }         
            return capitalized;
         }
         modelCtrl.$parsers.push(capitalize);
         capitalize(scope[attrs.ngModel]);  // capitalize initial value
     }
   };
});

HTML like -

<input type="text" ng-model="name" capitalize>
Community
  • 1
  • 1
ngLover
  • 4,439
  • 3
  • 20
  • 42
0

The first Answer works fine. But I've solved this with this CSS code:

CSS:
#article-id {
    text-transform: uppercase;
}
yuro
  • 2,189
  • 6
  • 40
  • 76