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 8003879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:46:05+00:00 2026-06-04T16:46:05+00:00

I would like to use the following code to restrict input (to alphanumeric) into

  • 0

I would like to use the following code to restrict input (to alphanumeric) into a text field:

$('input').bind('keypress', function (event) {
    var regex = new RegExp("^[a-zA-Z0-9]+$");
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
    if (!regex.test(key)) {
       event.preventDefault();
       return false;
    }
});

The above code works great on a standard input field, but when I type into a field generated by Chosen, the function is not applied. The chosen plugin does generate a div containing <input type="text"> but $('input') doesn’t seem to touch it. I’m guessing this is due to the timing of when the above code is applied..

  • 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-06-04T16:46:07+00:00Added an answer on June 4, 2026 at 4:46 pm

    Jack’s answer works. But the important thing to understand is that the keypress event bubbles, so you can catch it anywhere up the node hierarchy, at a node that is not generated dynamically.

    http://jsfiddle.net/KWv7Z/6/

    $(document).bind('keypress', function (event) {
        // Will fire for all elements, we only care for inputs
        // Also allow navigation keypresses
        if (event.target.tagName.toUpperCase() != "INPUT" ||
                $.inArray(event.which, [8,9,13,16,17,18,19,20,27,33,34,35,36,37,38,39,40,45,46]) ) {
          return;
        }
        var regex = new RegExp("^[a-zA-Z0-9]+$");
        // jQuery already normalizes event.which
        var key = String.fromCharCode(event.which);
        if (!regex.test(key)) {
           event.preventDefault();
           return false;
        }
    });
    

    This is also more memory friendly since it doesn’t install a separate handler per input on the page.

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

Sidebar

Related Questions

I would like to know if it is safe to use the following code
I would like to know if I can use the following JQuery function on
I use the following code and I would like to be able to get
I would like to use the following code in C# but I just can't
I would like to use something like AutoMapper for the following code instead of
I'm using the following code successfully with HTTP but I would like to use
I would like to use following sql to avoid constructing sql dynamically: SELECT CommentID,
I'm writing my Servlet application and would like to use the following static method
I would like to use KVO in the following context: 1) In touchesBegan:withEvent: I
I would like to use something like the following: SELECT city FROM cities WHERE

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.