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

  • SEARCH
  • Home
  • 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 6386543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:02:22+00:00 2026-05-25T03:02:22+00:00

I am writing a javascript code to restrict the keys that can be entered

  • 0

I am writing a javascript code to restrict the keys that can be entered into a text box.

function keyRestricted(e) {
    var keypressed;
    var keychar;
    var keycheck;
    // IE - keyCode
    // Netscape/Firefox/Opera - which
    keypressed = e.keyCode || e.which;
    keychar = String.fromCharCode(keypressed);
    //alert(keychar);
    keycheck = /[a-zA-Z0-9\b]/;
    return keycheck.test(keychar);
} //keyrestricted

my regex is now /[a-zA-Z0-9\b]/, which allows alphanumeric and backspace. I want to allow the delete, L/R arrows, and escape keys to work in firefox (3.6).

I am not sure sure what are the symbols for these keys.

In ie8, the escape key (and del/arrows) still is active even if the \e switch is excluded from the regex, when pressed, it resets/empties the text box.

In FF, I put the escape in the expression /[a-zA-Z0-9\b\e]/, but it does not seem to work for firefox, that is when the escape key is pressed, it does not reset/empty the text box.

what are the valid symbol for the regex to allow alphanumeric, L/R arrows, delete, escape?

Also, what is the translation for this [a-zA-Z0-9\-\_]?
It was meant to be alphanumeric and hyphens. But what is the slash infront of the hyphen since hyphen does not need a slash? and what is the \_ for since the underscore is not matched by the expression?

TIA

Edit

The reason why using keycode numbers as suggested by nnnnn did not work for me (for other people?) is because the keycodes from 65-90 are for uppercase letters, even though some websites do claim that those keycodes work for both lower and upper cases.

This http://www.lookuptables.com/ website shows that lowercase letters are from 97-122. Unfortunately, this range has some unmapped overlaps with some characters. For example, the \ character is listed as having 220 keycode. But my filter to deactivate of keycodes > 122 would still allow the \ to get through. There are other examples.

I have tested this using my laptop keyboard and an external full size keyboard.

Edit 2

I have combined both the regex and the keycode arguments into one function. The function works in principle, but due to crazy keycode conflicts, it does not work for the % sign. It requires both the onkeydown and onkeypress to catch all the keys (except the % key). See my discussion herejavascript regex for key event input validations troubleshooting help

<head>
<script type="text/javascript">
function keyRestricted(evt) {
    var theEvent = evt || window.event;
    var key = theEvent.keyCode || theEvent.which;
    var keychar = String.fromCharCode(key);
    //alert(keychar);
    var keycheck = /[a-zA-Z0-9]/;
    // backspace || delete || escape || arrows
    if (!(key == 8 || key == 27 || key == 46 || key == 37 || key == 39)) {
        if (!keycheck.test(keychar)) {
            theEvent.returnValue = false; //for IE
            if (theEvent.preventDefault) theEvent.preventDefault(); //Firefox
        }
    }
}
</script>
</head>
<body>
    Please modify the contents of the text field.
    <input 
        type="text" value="" 
        onKeypress="return keyRestricted(event)" 
        onKeydown="return keyRestricted(event)" 
    />
</body>
  • 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-25T03:02:23+00:00Added an answer on May 25, 2026 at 3:02 am

    As far as I know, Regex – or at least the JavaScript version – doesn’t let you test for some of those “special” characters like escape and the arrow keys (though I believe you can test for backspace).

    I prefer to implement this sort of thing with a standard if statement:

    var keypressed = e.which || e.keyCode;
    
    if ((keypressed >=65 && keypressed <= 90) // letters
        || (keypressed >=48 && keypressed <= 57) // digits
        || keypressed === 8 // backspace
        || keypressed === 27 // escape
        || keypressed === 46 // delete
        || (keypressed >= 35 && keypressed <= 40) // end, home, arrows
        // TODO: shift, ctrl, alt, caps-lock, etc
        ) {
      // do something
    }
    
    // If the keys you care about don't follow any particular pattern
    // a switch might be more convenient:
    switch (keypressed) {
      case 8:
      case 27:
      case 46:
         // valid key, do something
         break;
      default:
         // invalid key, do something else
         break;
    }
    
    
    // You can also do something like this:    
    var permittedKeyCodes = {
      "8" : true,  // backspace
      "27" : true, // escape
      "46" : true  // delete
    };
    if (permittedKeyCodes[keypressed]) {
      // do something
    }
    

    If you use the latter approach, it would be more efficient to define the permittedKeyCodes object outside your function.

    There are various places (here’s one) where you can get a list of all of the keycodes.

    Note that if you’re trapping the keydown or keyup event the keycodes returned are associated with the keys, not the characters, so e.g., upper and lowercase A both have the same code. The keypress event works differently.

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

Sidebar

Related Questions

I'm writing javascript code for a web emulator containing function keys. The user is
I'm writing a piece of client-side javascript code that takes a function and finds
I'm writing some JavaScript code to parse user-entered functions (for spreadsheet-like functionality). Having parsed
I'm writing some Javascript code that generates an XML document in the client (via
While writing JavaScript code, I Separate each code block with <script> tags <script type=text/javascript>
From time to time, I find myself writing server code that produces JavaScript code
I'm writing some MVC JavaScript code using Prototype. The problem is that when I
I'm writing code that calls an external JavaScript library Foo but only if Foo
I use only jQuery for writing JavaScript code. One thing that confuses me is
I am writing some JavaScript code that uses a string rendered with PHP. How

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.