Textbox to accept only numbers (digits) using jquery

Few days back, my friend Parleen asked me how can we make a textbox which just accepts only numbers specially digits only. And, for his I come up with this solution of textbox which only accepts digits, and if you try to enter any alpha bates in it then it displays the error message with animation.

View Demo


<input type="text" name="quantity" id="quantity" /> <span id="errmsg"></span>

As you can see above, I’ve given the name and id of textbox to “quantity” in this example.This is the textbox which only accepts numbers (digits only). You can see “span” after textbox which is used to display the error message with fading effect using jQuery.

Javascript Code

First of all, we need to use jQuery library as we’re using the jquery’s function to accept only digits.

<script type="text/javascript" src="jquery.js"></script>

Now le’ts write the code in JavaScript using jQuery to accept only digits in textbox and displaying error with animation.

//when key is pressed in the textbox$("#quantity").keypress(function (e)
  //if the letter is not digit then display error and don't type anything
  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
    //display error message
    $("#errmsg").html("Digits Only").show().fadeOut("slow");
    return false;

When the key is pressed, we’re using the key’s ASCII value to check which button is pressed. In first expression, delete, tab or backspace button is is checked and “8” is the ASCII values of the Back-space. Digits are checked in the second expression. “48” is the ASCII values of “0” and “57” is the ASCII values of “9”. The the ASCII values of the other digits lies between “48” to “57”. And, if the key pressed values doesn’t lies withing these range, then we are displaying the error message with jQuery’s fading effect.

And, the “return false” statement means that this functions returns false values which means not to type anything on the text box.

Download Full Source Code

40 thoughts on “Textbox to accept only numbers (digits) using jquery

  1. Nice little script, it can be very useful for developers, and it provides the user with immediate feedback rather than waiting until they click “submit” and giving them the message then. I’ll definitely have to use it, thanks!

  2. You are always welcome gabe…

  3. sunless

    What about copy/paste or mark/drag/drop text with mouse?

  4. oh right…..you can write the condition to do that

  5. Konr

    And continuing on with sunless’s response, it should allow a Ctrl+V for digits. But it doesn’t because the V isn’t a digit.

  6. Ian

    Along the same lines of what Konr pointed out, none of my other browser keyboard shortcuts work as long as the focus is in that text box. Ctrl+W, for example, does not close the tab.

  7. Nikolay

    Very good solution !
    I’ll be using this, thnx !!

    bug: works shift+ins (paste any text)


    $(el).keypress(function (e)
    //if the letter is not digit then display error and don’t type anything
    if((e.shiftKey && e.keyCode == 45) || e.which!=8 && e.which!=0 && (e.which57))
    //display error message
    $(“#errmsg”).html(“Digits Only”).show().fadeOut(“slow”);
    return false;

    next bug: ctrl+v and shift+ins allowed in Safari 3.02
    I can’t fixed it because of the peculiarities Safari event keyCode’s

  8. Good job Nikolay…….nice to hear that….

  9. Niel

    Nice little script, beside ctrl+v or shift+ins i found another bug if user pasted alpha chars using right click then pasting it, it doesn’t prevent user from that…

  10. ryan john majarais

    thanks for the code….

  11. This is cool stuff, though I knew it already last year and there was no jQuery then.
    good work anyway :)

  12. oh thanks anyway fedmich….

  13. no more annoying error message
    just replace space & alphabet with null

    $(‘.numberonly’).keyup(function(e) {
    if(e.which==13) return false;
    c = $(this).val().replace( /[A-Za-z\s]/g ,”);

  14. Jay

    Awesome script, but whenever I try to hit enter to submit the form it shows the error of “Digits Only” how can I fix this?

  15. Thanks helped a lot.

  16. It’s really cool to have this script, thank you.

    It does seem though that there should be an easier way to do this in jquery because it’s so common.

    For example in most desktop development environments you toggle an option and get this feature.

    Yes, I know JQuery and related script has to remain super light, but still…


  17. Thanks was looking for something like this.

  18. Adem

    Very nice code, thank you.
    Two questions will be.
    How we will use multi-input?
    Input1: errmsg1:
    Input2: errmsg2:
    Input3: errmsg3:
    Input4: errmsg4:

    Requirement to start with zero?
    Example: 0123, 056784, 045674, 09876, such as.

    I am beginner, please try the code I would like


  19. nishit

    thank u rohan …i m liking for this validation from long time ……thankyou again

  20. Mani

    Thanks ur support
    not supporting server control

  21. d

    This script only works for numbers at the top of your keyboard. NumPad numbers wont work.

  22. Charlie Elliott

    Any help on checking multiple textboxes with only one function?

  23. Waqas

    Dear Roshan
    I have just copied n pasted to ma test page. but it is not working for me. Can you please help me. I m new to web development.
    it looked very nice. but not working for me. dont know…

    i included the js file.
    i my aspx page.

    copied ur html code i.e

    n wrote the javascript like

    $(“#quantity”).keypress(function (e)
    //if the letter is not digit then display error and don’t type anything
    if( e.which!=8 && e.which!=0 && (e.which57))
    //display error message
    $(“#errmsg”).html(“Digits Only”).show().fadeOut(“slow”);
    return false;

    i just copied your code.
    But it is not working..what should i do???
    please help if you can

  24. Shachindra Pandey

    Hey it is really fantastic code.I am using this concept in my work.
    Really an amazing experience.wonderfull thanks buddy thank you very much.

    sachindra Pandey

  25. Nate

    Has this been tested in all the major browsers? How does this compare the the numeric jquery plugin?

  26. zeally

    Thank you, it’s work.

  27. ali

    This is great code.. great work man!!!!… but there are still some problems ..
    1. You can paste text by right click on the text box and paste .
    2. Please generalize code by passing ID of the control as parameter.
    Ali Raza

  28. mj


    How can i change the code to accept alphabets and space only?


  29. dm777

    Very nice script.

    How to I allow it to accept period, decimal characters both . or ,


  30. Alex Lam

    hello everybody, i just do like Waqas, but it do not work. I don’t know the error it is. Can you please explain “keypress(function (e)”. What is “e”? Thanks so much.

  31. dinesh

    Hi Roshan your code really superb. Is any code to accept the numbers with dot ( I mean float number) If there means please help me.

  32. jumpjack

    How can I prevent user IN ADVANCE from inserting characters other than numbers? For mobile devices users it would be much more comfortable if the only input possibile is numeric, as they must press each button 3 or 4 times to get a number rather than a letter!
    I tried adding TYPE=”TEL” or format=”*N” in the input field, but it does not work.

  33. MHD

    Thank you very match .. it’s help me.

  34. Thanks for given me a good guide about it, it has helped me a lot.

  35. syed mohammed ahmed

Leave a Reply

Your email address will not be published. Required fields are marked *