lundi 21 janvier 2019

How to set maximum length of input tag using jquery?

I have a number pad and . 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){

            }//if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);
            // $(".num-text").attr({
            // "max" : 10,        // substitute your own
            // "min" : 10          // values (or variables) here
            // });
        });

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

Aucun commentaire:

Enregistrer un commentaire