[RESOLVED]How to prevent particular letter using jquery

Hi,

in my application i want to prevent pressing control+U .

when page loads and anyone press control button and letter U . then display an alert box.

how it is possible using jquery.

Regards

Baiju

$(document).keypress("u",function(e) {
  if(e.ctrlKey)
    alert("Ctrl+U was pressed!");
});

Hi Baiju,

Thanks for your post.

klbaiju

in my application i want to prevent pressing control+U .

As for your problem, you could refer to the following sample.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function Disable_Control_C() {
            var keystroke = String.fromCharCode(event.keyCode).toLowerCase();
            if (event.ctrlKey && (keystroke == 'u' || keystroke == 'v')) {
                alert("YOU pressed Ctrl+U!");
                // disable Ctrl+U
                event.returnValue = false; 
            }
        }
    </script>
</head>
<body onkeydown="javascript:Disable_Control_C()">
    <form id="form1" runat="server">
        <div>
            Hello World!
        </div>
    </form>
</body>
</html> 

Hope it will be helpful to you.

Best Regards,

Fei Han

Hi Smirnov,

your code is working fine in firefox and explorer.and not in chrome.

what i have to change this for working in chrome

klbaiju

what i have to change this for working in chrome

You can try with the below code

   // A $( document ).ready() block.
$( document ).ready(function() {
   var isCtrl = false;
document.onkeyup=function(e){
    if(e.which == 17) isCtrl=false;
}
document.onkeydown=function(e){
    if(e.which == 17) isCtrl=true;
    if(e.which == 85 && isCtrl == true) {
        //run code for CTRL+U 
        alert('Your Pressed CTRL + U Key combination');
        return false;
    }
}
});

Complete Code:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
 <script>
   // A $( document ).ready() block.
$( document ).ready(function() {
   var isCtrl = false;
document.onkeyup=function(e){
    if(e.which == 17) isCtrl=false;
}
document.onkeydown=function(e){
    if(e.which == 17) isCtrl=true;
    if(e.which == 85 && isCtrl == true) {
        //run code for CTRL+U 
        alert('Your Pressed CTRL + U Key combination');
        return false;
    }
}
});
   </script>
</head>
<body>

</body>
</html>

Hi A2H ,

thanks for your reply.ur code is not working in firefox and chrome.

following code is working in firefox and IE

 $(document).keypress("u", function (e) {
        if (e.ctrlKey) {

            return false;
        }
       
    });

but it is not working in chrome. can you correct the above code for chrome

This works in both, IE and Chrome. 

var isCtrl = false;
document.onkeyup=function(e){
    if(e.which == 17) isCtrl=false;
}
document.onkeydown=function(e){
    if(e.which == 17) isCtrl=true;
    if(e.which == 85 && isCtrl == true) {
        //run code for CTRL+U -- ie, whatever!
        alert('CTRL + U stuff');
        return false;
    }
}

Source: http://stackoverflow.com/questions/13394189/bind-ctrl-u-keystroke-in-jquery 

Demo: http://jsbin.com/wohujiguxiwa/1/ 

klbaiju

Hi A2H ,

thanks for your reply.ur code is not working in firefox and chrome.

The code which I provided above will work fine in IE,Chrome and Firefox. I have tested it in all browsers.Please try it once again.

Hi smirnov finally i find the error. change keypress to keydown it will work

$(document).keydown("u", function (e) {
         if (e.ctrlKey) {           

             return false;
            
         }

     });

this will work in ff,ie,chrome

Leave a Reply