0

Using the directive in How to limit input[number] cause a issue.

When in input reached max I want to mark the input(double right click on the input) and then pressing a digit to make it change the entire input don't change it, due to the directive e.preventDefault(); The selection event can be view in:

angular.element(elem).on("select", function(e) {
                console.log(e);

            });

What is the best way to fix it? The directive:

angular.module('myApp')
  .directive('limitTo', limitTo);

function limitTo() {    
    return {
        restrict: "A",
        link: function(scope, elem, attrs) {
            var limit = parseInt(attrs.limitTo);
            angular.element(elem).on("keypress", function(e) {
                if (this.value.length == limit){
                    e.preventDefault();
                } 
            });
        }
    }
};

And use:

    <input type="number" class="form-control form-control-lg"  limit-to="6" 
    ng-model="vm.details.m_inputSMS" placeholder = {{vm.configProprs.kodPlaceHolder}} name ="m_inputSMS" id="m_inputSMS"
    ng-model-options="{ updateOn: 'blur' }"required/>
Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114

2 Answers2

0

So to solve this issue I used window.getSelection().toString() and make sure the selection match the input:

if(this.value.length == limit && window.getSelection().toString() == this.value){
     //logic goes in here...
}
Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114
0

If you want to give limit to input field then using ng-minlength="6" ng-maxlength="6" this you can assign limit in angularjs.

<input type="number" class="form-control form-control-lg"  ng-minlength="6" ng-maxlength="6"
    ng-model="vm.details.m_inputSMS" placeholder = {{vm.configProprs.kodPlaceHolder}} 
    name ="m_inputSMS" id="m_inputSMS" ng-model-options="{ updateOn: 'blur' }"required/>
Nikita
  • 437
  • 2
  • 12