Enter Only Numbers Using JavaScript Example

Updated on     Kisan Patel

This post will show you how to restrict the user to allow only numbers in input textbox using javascript.


function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        return true;


Enter Number: <input type="text" class="groupOfTexbox" onkeypress = "return isNumber(event)"/>

the output of the above example…

See the Pen jbGooo by Kisan (@pka246) on CodePen.


Leave a Reply