Allow user to enter only numbers in textbox using jquery or javascript

We will do this using codes of the alphabets & numbers

Lets say bellow is my textbox

<input type="text" id="numText" runat="server" />

to allow only numeric or number we need to check code of entered character & according to that we need to make decision.
So bellow is jquery for that.

                $("#numText").keydown(function (event) {
                    var keycode = event.keyCode;
                    //comparing pressed keycodes
                    if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57)) {
                        return false;
                    else {
                        return true;

Like this you can allow only numbers to be entered in textbox using jquery


  • yassershaikh

    Such a buggy code my Fhreendd –
    1. Ctrl + A, Ctr+C, Ctr + V does not work on the textbox now
    2. Home end basic editing also fails
    3. ‘@#$@#$@$#@$#@$#@#%$#’ these characters are allowed.

    shya shah !! rectify this bro :)