Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7072543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:49:17+00:00 2026-05-28T05:49:17+00:00

I need to set up an <input type=text /> so that it will accept

  • 0

I need to set up an <input type="text" /> so that it will accept only numeric chars, backspace, delete, enter, tabs and arrows.

There’s a lot of exemple around there, i started with something similar to this:

function isNumericKeyCode (keyCode){
    return ( (keyCode >= 48 && keyCode <= 57) //standard keyboard
           ||(keyCode >= 96 && keyCode <= 105)) //Numpad
}

$('#myTextBox').keydown(function(e){
         var handled = true;
         var keyCode = e.keyCode;
         switch(keyCode){
            //Enter and arrows
            case 13:
            case 37:
            case 38:
            case 39:
            case 40:
               doSomethingSpecialsWithThesesKeys();
               break;
            default:
               handled = false;
               break;
         }

         if (  !handled
            && keyCode !== 8 //backspace
            && keyCode !== 9 //tab
            && keyCode !== 46 //del
            && !isNumericKeyCode(keyCode)){

            handled = true;
         }

         return handled;
});

All that worked perfectly until I hit the “#” key. In my french canadian keyboard, the “#” has his own key (no shift implied) that returns keyCode 51, the same as the number “3”.

I think that in US keyboard, the “#” is obtained by pressing shift+3, that may be why they have the same keycode.

Now I realize that I have to handle the shift and alt keys too, but that’s another story.

It works differently with the jquery keypress event, which offer the charCode property, but I did not used it at first because of what the documentation says :

as the keypress event isn’t covered by any official specification, the
actual behavior encountered when using it may differ across browsers,
browser versions, and platforms.

Also, I would need a workaround in that case to handle tabs, arrows and other special keys since they don’t provide a charCode.

So the question is :
is there a way to allow only some specifics chars using the keydown event? And that, in a way that will work independently of the keyboard layout?

As a side quest : Which browsers may be problematics with the keypress event? I mean, currently I don’t really care if my website does not support IE6. I am targetting recent browsers.

Edit


As someone pointed out in the comments, this method does not allow user to “ctrl+v” a number in the input. In my particular case this is really not a requirement to be able to paste a number. But this popped something in my head, the user still can right-clic > copy some text in the input, and in that case that could be anything. The more I think of it, the more it seems to me that I will need the keydown event to handle tabs and arrows, and another event to handle the input itself.

Edit2


A lot of beautiful answers here, but the award goes to mrtsherman for the use of input and propertychange events. I will use a combination of this answer for the numeric validation, plus the keyCode event as before for the special use of arrows, tabs and enter keys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:49:18+00:00Added an answer on May 28, 2026 at 5:49 am

    How about something like this. This should cover cut/paste and also rmb content. We monitor the textbox for any change in content. Then we use a regex to filter out characters based on a whitelist. This won’t handle non-character key, but I think that is okay.

    The \d flag says that only digits should be accepted.

    http://jsfiddle.net/UXeva/1

    $('#myTextBox').bind('input propertychange', function() {
        var text = $(this).val();
        if (isNaN(text)) {
           $(this).val(text.replace(/[^\d]/gi, ''));
        }
    });
    

    We bind to two events here. input for FireFox and propertychange for other browsers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an input which it's type is set to hidden, I need to
I have a set of html text boxes that take input and when the
I need to match input strings (URLs) against a large set (anywhere from 1k-250k)
I need to loop through a set of 2-dimensional array of hidden input fields
I need Set collection, where its items will be identified by items class. Something
I need to set the text within a DIV element dynamically. What is the
I need to set up this scenario: A SQL Server 2005 database will create
Having the following hierarchical text data input (JunOS-like, in fact) I need to parse
I recently have come across with a need for some type of translation-type that
Is it possible to get the current input type (text or numbers) from keyboard?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.