Jquery allow only number up to 10 digit

jquery-allow-only-number

In this blog, we will see how can allow up to 10 digits numbers, basically this script will help you with the number box input.

I will use keypress event so that I can capture the things on keypress, and I will also jquery attr method to add the min attribute by using the jquery.

Below is the HTML code.

<input type ="number" class="allow-only-number"/> 

Below is the script which helps you to allow only 10 digit numbers to the input field, it will also block negative and e keywords.

$(".allow-only-number").keypress(function (e) {
        $(".allow-only-number").attr("min", "10");
            var valueLength = this.value.length;
            if (valueLength == 10) {
                return false;
            }
            var kk = e.which;
            if (kk < 48 || kk > 57)
                e.preventDefault();
    });

I hope the above small query will help you to resolve your issue, happy coding.

0 0 votes
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Spread the love
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x