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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:13:38+00:00 2026-06-03T04:13:38+00:00

I have this jQuery which stops the enter key from being pressed and I

  • 0

I have this jQuery which stops the enter key from being pressed and I have prepared it to accept an array of disallowed keypresses..

    $('.form input').bind('keypress', function(e) {
        var keys = [13];
        for (var i = keys.length; i--;){
            if(e.keyCode===keys[i]){
                return false;
            }
        }
    });

I want to do similar thing with the | character, but since it is shift of 220 and I don’t need to stop \ from being entered into the form, how do I restrict that character or use a modifier key? (will also be processing it out server-side, of course).

I’m also aware that keyboard layout for special characters may differ from keyboard to keyboard with different localization, so it may be necessary to focus on the resulting character rather than the key itself (not entirely sure on that), but I don’t want to introduce a large amount of overhead

  • 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-03T04:13:39+00:00Added an answer on June 3, 2026 at 4:13 am

    The keypress event is about characters, not keys. You can just compare keyCode to the character code for "|" ("|".charCodeAt(0)) directly, no need to worry about the shift key being down (and it may not be on all keyboards).

    Example – live copy | source:

    HTML:

    <p>Try to type | in the box below.</p>
    <input id="theInput" type="text" size="80">
    

    JavaScript:

    jQuery(function($) {
    
      var keys = [13, "|".charCodeAt(0)];
      $("#theInput").keypress(function(e) {
        var index;
    
        for (index = 0; index < keys.length; ++index) {
          if (keys[index] === e.keyCode) {
            display("Denied!");
            return false;
          }
        }
      });
    
      function display(msg) {
        $("<p>").html(msg).appendTo(document.body);
      }
    });
    

    Or as bažmegakapa points out, since you’re already using jQuery, you can use its inArray function:

    jQuery(function($) {
    
      var keys = [13, "|".charCodeAt(0)];
      $("#theInput").keypress(function(e) {
        if ($.inArray(e.keyCode, keys) !== -1) {
          display("Denied!");
          return false;
        }
      });
    
      function display(msg) {
        $("<p>").html(msg).appendTo(document.body);
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jQuery which I need adapting: $(document).ready(function(){ $(.rss-popup a).hover(function() { $(this).next(em).stop(true,
I have this jquery script which suppose to hide/show div element base on the
On my main page I have this jquery code which does an ajax call
i have this javascript code to which i want to add the jquery fade-in
We have this anonymous function in our code, which is part of the jQuery's
I have jQuery code which looks something like this on Button1 Click $('table.result_grid tbody
Ok so i have a text field that uses this jQuery watermark plugin which
I have this jQuery: $(document).ready(function() { $(#panel).hide(); $('.login').toggle( function() { $('#panel').animate({ height: 150, padding:20px
I have this jQuery datepicker code that initially set the minDate of the datepicker.
I have this JQuery code working however I need to know whether how to

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.