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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:24:41+00:00 2026-06-04T21:24:41+00:00

I borrowed a bit of code from this question (see reply 4): how do

  • 0

I borrowed a bit of code from this question (see reply 4):

how do i block or restrict special characters from input fields with jquery?

However, upon using the code, it appears, that although it does the validation as you type neatly, it does not allow you to edit anything you have entered.

$('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;
}});

I am a bit confused, I have tried to adjust it by changing the keypress to keyup but this just stops the validation from working. Any pointers would be graciously accepted.

  • 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-04T21:24:43+00:00Added an answer on June 4, 2026 at 9:24 pm

    This seems to be a browser specific issue. In Firefox keypress includes meta keys like backspace and the arrow keys while Chrome does not. If you adjust your logic a bit to prefer which over charCode (which I think is better supported anyways) you end up with this

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

    This fiddle also writes to console the keys and codes being passed in the event when typing.
    http://jsfiddle.net/Q2MWS/5/

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

Sidebar

Related Questions

Take the code below, adapted from this question : //Borrowed from another question because
I have borrowed code from this link PHP regex templating - find all occurrences
#&q=car&category=Car%20Audio%2CAccessories&brand= I borrowed a this function from a previous question asked on SO: function
Title borrowed from this question , of which this one is not a duplicate.
This following code example is borrowed from MSDN here . I am not getting
The following gist: https://gist.github.com/1876791 (borrowed from the demo in this question ) is a
This is borrowed from another question I made on the site, but would have
I borrowed this from another question about fragmentation, but I'm not bothered by that.
I am using a bit of borrowed code, and am concerned for its vulnerability
I 'borrowed' a regex from this website : http://daringfireball.net/2010/07/improved_regex_for_matching_urls that is almost complete but

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.