Input type NUMBER does not consider maxlength

Asked

Viewed 5,749 times

3

I have the following HTML:

<input ng-model="idade" type="number" maxlength="3" placeholder="Informe a idade">

When I go to test, the input ignores the maxlength and lets you type as many characters as you want. Strangely, when I change the type to tel it works.

Someone’s been through it and they’ve solved it???

  • tries 'ng-maxlength'

  • What is the goal, which size of the value is equal to 3 digits (999) or the maximum value is 3? Type number will not put a mask keydown in the element - if you want to limit the maximum input to 999, for example, you will have to use a function.

  • I had already tried, and it had not worked.....

4 answers

11


According to the MDN documentation, you can only use the maxlength for the guys text, email, search, password, tel, or url.

That was a design decision.

If you wish, you can use the min and max attributes to validate numbers. (remembering that this validation, by default, will be performed in Ubmit and not at the time of typing).

  • So for me to do this, I’ll have to use jquery, right? Since min and max do not meet my requirements...

  • @fernandoocf jQuery is an option, but not the only one. What you use is up to you, because you know your system better than us :p.

  • The criterion is the same as maxlength.... only allow the user to only type up to 3 characters in the field, without being able to go beyond that....

  • I switched to type="tel" and solved my problem =)

  • @fernandoocf Also is a solution. As I said, only you know what is best for you :p

3

You can include a "mask" in the event keydown of input. Something like:

document.getElementById("numero").addEventListener('keydown', function(event) {

  if (this.value.length == 3 && event.keyCode != 8) {
    event.preventDefault();
    return false;
  }

});

function soNumeros(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /[0-9]|\./;
  if (evt.keyCode)
    return true;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}
<input id="numero" ng-model="idade" type="number" maxlength="3" placeholder="Informe a idade" onkeypress='soNumeros(event)'>

3

You can use your own Angular to create a directive:

var app = angular.module("app", []);

app.directive('ngMax', function() {
  return function(scope, element, attrs) {
    angular.element(element).on("keypress keydown", function(e) 
    { 
      if (e.keyCode != 8 && this.value.length == attrs.ngMax) 
      {  
         e.preventDefault();
         return false;
      }
    });
  };
});

app.controller("ctrl", ["$scope",
  function($scope) {

  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div ng-app="app" ng-controller="ctrl">
  <input ng-model="idade" 
         type="number"
         ng-maxlength="3" 
         ng-max="3" />
</div>

Was created the Directive ngMax which can be used with the maximum number configuration.

  • Legal the directive, but if I miss age I have to refresh the page to fix it?

  • @Diegoaugusto I put ng-maxlength which has a validation character and the directive created to play the role of a mask. No... of course depends on your context, but, no...

  • I understand, is that in this example Backspace does not work

  • @Diegoaugusto is number!? It’s not number!? (by number logic has no space, only if its rule is another)

  • Yeah, I don’t think you understand. If I type an age x and try to delete to type another age input is blocked, in case I would have to refresh the page to type another age.

  • If I want to edit it too only after a refresh

  • @Diegoaugusto I just tested in the example of the answer and it works the backspace (sorry earlier had not understood, example made for Html5 and angular equal your tags, has another purpose? ), but, reinforcing works.

  • It’s browser problem, Firefox does not work

  • @Diegoaugusto made the adjustments ... now it is within the

Show 5 more comments

0

The directive could be improved by allowing you to navigate within the field and also to be erased from any position.

app.directive('ngMax', function() {
    return function(scope, element, attrs) {
        angular.element(element).on("keypress keydown", function(e) { 
            if (e.keyCode != 8 &&
                e.keyCode != 9 &&
                e.keyCode != 35 &&
                e.keyCode != 36 &&
                e.keyCode != 37 &&
                e.keyCode != 39 &&
                e.keyCode != 46 &&
                this.value.length == attrs.ngMax
            ) {
                e.preventDefault();
                return false;
            }
        });
    };
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.