How to set maximum length of input tag using jquery?

jquery limit input length
set maxlength for input type=number jquery
jquery maxlength validation
textarea character limit jquery
javascript limit input length
how to set text limit in html
input text max length number
bootstrap input max length

I have a number pad and <input> .

I am typing the Mobile number in the input tag using the number pad below I want set the maximum length of the input to 8 I used several method but of them the maximum length will work only if type from the keyboard not working when I use the number pad Can some one help me

I have tried

$(".num-text").attr({
  "max" : 10,        
  "min" : 10          
});

This is my code

html
    <div id="zeroseven">07<input id="numBox" class="num-text" name='tel' oninput="javascript:  this.value = this.value.slice(0, this.maxLength);" type = "number" maxlength = "8"/></div><br><br>
<div class="numpad">
        <button class="numbers" value="1">1</button>
        <button class="numbers" value="2">2</button>
        <button class="numbers" value="3">3</button>
        <button class="numbers" value="4">4</button>
        <button class="numbers" value="5">5</button>
        <button class="numbers" value="6">6</button>
        <button class="numbers" value="7">7</button>
        <button class="numbers" value="8">8</button>
        <button class="numbers" value="9">9</button>
        <button id="clear" class="numbers" >&larr;</button>
        <button class="numbers" value="0">0</button>
        <button id="delete" class="numbers">DEL</button>
</div>
jquery
         //Onlick numpad button
        $( ".numbers" ).on('click',function(evt) {
            $(".num-text").val(($('.num-text').val()) + (this.value));
            var length = $('.num-text').val().length;
            if (length > 8){

            }
        });

I need it so that if the length >8 it should stop typing more numbers after that

You have done 99.9%

 $(".num-text").val(($('.num-text').val()) + (this.value)); 

Just put this into if case

        $( ".numbers" ).on('click',function(evt) {            
            var length = $('.num-text').val().length;
            if (length < 8){
            $(".num-text").val(($('.num-text').val()) + (this.value));
            }
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="zeroseven">07<input id="numBox" class="num-text" name='tel' oninput="javascript:  this.value = this.value.slice(0, this.maxLength);"
            type = "number"
            maxlength = "8"/></div><br><br>
            <div class="numpad">
                    <button class="numbers" value="1">1</button>
                    <button class="numbers" value="2">2</button>
                    <button class="numbers" value="3">3</button>
                    <button class="numbers" value="4">4</button>
                    <button class="numbers" value="5">5</button>
                    <button class="numbers" value="6">6</button>
                    <button class="numbers" value="7">7</button>
                    <button class="numbers" value="8">8</button>
                    <button class="numbers" value="9">9</button>
                    <button id="clear" class="numbers" >&larr;</button>
                    <button class="numbers" value="0">0</button>
                    <button id="delete" class="numbers">DEL</button>
            </div>

Setting maxlength of textbox with JavaScript or jQuery, $('input[name="someelement"]').attr('maxlength', 100);. or $('input.someclass').attr​('maxlength', 100);. or like tanaydin said : Include the jQuery library to validate input field length using jQuery. Based on the minimum (minLength) and maximum (maxLength) value the input length is validated. If the length exceeds, the additional value will be removed. The validation message is shown in the < span > tag.

As per your code, you can use below logic.

$(document).ready(function() {
  $(".numbers").on('click', function(evt) {
    var length = $('.num-text').val().length;
    if (length >= 8) {
      evt.preventDefault();
    } else {
      $(".num-text").val(($('.num-text').val()) + (this.value));
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="zeroseven">07<input id="numBox" class="num-text" name='tel' oninput="javascript:  this.value = this.value.slice(0, this.maxLength);" type="number" maxlength="8" /></div><br><br>
<div class="numpad">
  <button class="numbers" value="1">1</button>
  <button class="numbers" value="2">2</button>
  <button class="numbers" value="3">3</button>
  <button class="numbers" value="4">4</button>
  <button class="numbers" value="5">5</button>
  <button class="numbers" value="6">6</button>
  <button class="numbers" value="7">7</button>
  <button class="numbers" value="8">8</button>
  <button class="numbers" value="9">9</button>
  <button id="clear" class="numbers">&larr;</button>
  <button class="numbers" value="0">0</button>
  <button id="delete" class="numbers">DEL</button>
</div>

How to change maxlength of certain element using jquery/javascript , How do you set the maximum length of an input type number? Use following jQuery plugin for setting maxlength of any textarea on your page. First thing for doing is to add maxlength="XX" attribute in your <textarea> tag. <textarea maxlength="15" rows="5" cols="30" name="address"></textarea> Include the jQuery plugin javascript file in your html page where the textarea is.

Min and max attribute does not work for html5 input type number <input type="text" pattern="\d*" maxlength="10" /> and also attach to javascript event to it to ensure the value is a number as a fallback to the pattern attribute

HTML, Tip: To set or return the width of a text field, in number of characters, use the size property. Browser Support. Property. maxLength, Yes, Yes, Yes, Yes, Yes  The maxlength attribute specifies the maximum number of characters allowed in the <input> element. Browser Support The numbers in the table specify the first browser version that fully supports the attribute.

How to limit the number of characters allowed in form input text field?, An <input> element with a maximum length of 10 characters: <form action="/​action_page.php"> <label for="username">Username:</label> <input type="text"​  Here, we’ve set the max length to 50 characters (you can set it to anything you like) and we monitor the amount of characters within the textarea using the keypress method. We also calculate the amount left by subtracting the current length from the max length, and use the html method to add that number to our code.

Text input length limit with jQuery, The max length functionality can easily be added to a textarea with appropriate default settings. You can also remove the max length functionality if it is no longer​  The ASPSnippets MaxLength jQuery has the following required and optional parameters: 1. MaxLength (required) – Integer value indicating the Maximum character length limit. 2.

jquery - check length of input field?, Yet another jQuery max length plugin which limits the number of Supports textarea, input field and any inline elements. Wrap your text fields into DIV containers and specify the maximum 2, < span class = "text-limit" >  Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.