4

I have an input box.

I need to autocapitalize this box.

Is there any properties avaliable for this or do I need to code on controller

i also need that this effect is applyed in angular model

Coder
  • 240
  • 2
  • 4
  • 20

7 Answers7

2

Yes, we have a directive for this :) The original code (written in TypeScript) is on GitHub https://github.com/ST-Software/STAngular/blob/master/src/directives/SgUpperCase.ts

//Fixed bug with cursor jumping at the end

someModule.directive("sgUpperCase", [function () {
    function getCaretPosition(inputField) {
        // Initialize
        var position = 0;
        // IE Support
        if (document.selection) {
            inputField.focus();
            // To get cursor position, get empty selection range
            var emptySelection = document.selection.createRange();
            // Move selection start to 0 position
            emptySelection.moveStart('character', -inputField.value.length);
            // The caret position is selection length
            position = emptySelection.text.length;
        }
        else if (inputField.selectionStart || inputField.selectionStart == 0) {
            position = inputField.selectionStart;
        }
        return position;
    }
    function setCaretPosition(inputElement, position) {
        if (inputElement.createTextRange) {
            var range = inputElement.createTextRange();
            range.move('character', position);
            range.select();
        }
        else {
            if (inputElement.selectionStart) {
                inputElement.focus();
                inputElement.setSelectionRange(position, position);
            }
            else {
                inputElement.focus();
            }
        }
    }
    return {
        require: "^ngModel",
        restrict: "A",
        link: function (scope, elm, attrs, ctrl) {
            var toUpperCase = function (inputValue) {
                if (!inputValue)
                    return inputValue;
                var modified = inputValue.toUpperCase();
                if (modified !== inputValue) {
                    var position = getCaretPosition(elm[0]);
                    ctrl.$setViewValue(modified);
                    ctrl.$render();
                    setCaretPosition(elm[0], position);
                }
                return modified;
            };
            ctrl.$parsers.push(toUpperCase);
            toUpperCase(scope[attrs.ngModel]); //Transform initial value
        }
    };
}]);
David Votrubec
  • 3,968
  • 3
  • 33
  • 44
2

You could use angular filter uppercase on change of that field using ng-change, If you want to make it capital on load then you should to same thing in ng-init too

Markup

<input type="text" ng-model="sample" ng-change="sample=(sample|uppercase)"/>
Tushar
  • 85,780
  • 21
  • 159
  • 179
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
1

Add this in your controller. $watch will listen for any changes made on bic and will update the value.

$scope.$watch('bic', function () {
    if ($scope.bic) {
        $scope.bic = $scope.bic.toUpperCase();
    }
});
Tushar
  • 85,780
  • 21
  • 159
  • 179
0

You can actually do this with CSS.

input{
    text-transform: uppercase;
}
<input type="text" value="This text is auto capitalized" />
SeeDoubleYou
  • 1,253
  • 2
  • 15
  • 25
  • in my code it captlize first word only – Coder May 07 '15 at 09:40
  • @Ervikas, what browser are you on? – SeeDoubleYou May 07 '15 at 09:42
  • the CSS transform does transform the value of the field, so a normal form submit would contain the capitalized values. However, David Votrubec mentioned in another comment it does not change the underlying value of the model in angular – SeeDoubleYou May 07 '15 at 09:47
  • Whilst this may work, transforming the input value via CSS would not be reflected in the angular `model`. For the uppercasing to be permanent it must be applied by Angular. – Ed B May 07 '15 at 09:47
  • yes you are right @EdB . it does not effect on model in need to send also data in uppercase to controller – Coder May 07 '15 at 09:50
0

You dont need Jquery/Javascript for this one. Try this:

<input type="text" ng-model="abc"   id="myid" class="span6" value="" name="">

In CSS

input {
    text-transform: uppercase;
}

JSFIDDLE DEMO

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • I kind of like this, it's very unobtrusive, although you'll need to apply the transform in JS too afterwards if you want to have your string actually be uppercase. (But what does jQuery have to do with anything?) – Matti Virkkunen May 07 '15 at 09:44
  • @MattiVirkkunen:- Agree, I said about jquery since the question was tagged with it ;) – Rahul Tripathi May 07 '15 at 09:45
  • Oh, all right. I wish people would stop tagging everything as jQuery because with Angular you rarely need it... – Matti Virkkunen May 07 '15 at 09:46
0

Add class into input tag. .capitalize { text-transform: uppercase" }

i.e <input type="text" ng-model="bic" class="capitalize" id="txtbic" maxlength="11" class="span6" value="" name="">
User2
  • 1,293
  • 8
  • 17
0

Check out this its working.. it also updates model...

<div ng-app="HelloApp">
    <input type="text" ng-model="name" uppercased/> {{name}}        
</div>

angular.module('components', [])
   .directive('uppercased', function() {
    return {
        require: 'ngModel',        
        link: function(scope, element, attrs, modelCtrl) {
            modelCtrl.$parsers.push(function(input) {
                return input ? input.toUpperCase() : "";
            });
            element.css("text-transform","uppercase");
        }
    };
});

angular.module('HelloApp', ['components'])

Working model here

Gnanadurai A
  • 278
  • 1
  • 2
  • 15