Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character limit on input text

I want to my textbox to only allow numbers and also have a character limit on it.

Currently, I have the numbers working... Now I am having issues figuring out how to limit the characters.

Here is what I have...

JS:

app.directive('numbersonly', function () {
    return {
        restrict: 'A',
        link: function (scope, elm, attrs, ctrl) {
            elm.on('keydown', function (event) {
                if (event.which == 64 || event.which == 16 && elm.val().length > 4) {
                    return false;
                } else if (event.which >= 48 && event.which <= 57) {
                    return true;
                } else if (event.which >= 96 && event.which <= 105) {
                    return true;
                } else if ([8, 13, 27, 37, 38, 39, 40].indexOf(event.which) > -1 && elm.val().length > 4) {
                    return true;
                } else {
                    event.preventDefault();
                    return false;
                }
            });
        }
    }
});

Markup:

<input type="text" id="txtEmpAge" data-ng-model="newemployee.Age" class="form-control" numbersonly required />
like image 871
user4756836 Avatar asked Oct 30 '25 21:10

user4756836


1 Answers

In html input attribute length. we can use it in multiple forms:

Length, Minlength and maxlength

Try this

maxlength="5"
like image 135
krishna singh Avatar answered Nov 03 '25 01:11

krishna singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!