Restrict Shift Key using jQuery

There is a very basic requirement of restrict numbers in a textbox And we implement the code to restrict other characters than 0 to 9. But there is a loop hole that sometimes not checked, that is using Shift Key and number key combination, user can still type special characters like @,#,$ etc.Below jQuery code detects how to restrict or disable Shift Key.

$(document).ready(function(){ 
$("#txtNumbers").keydown(function(event) {
     if(event.shiftKey)
     {
        alert('You have pressed Shift Key.');
        event.preventDefault();
     }
   });
});
See live Demo and Code.

Above code uses keydown event to track shift key, if you use same method in keyPress event, then it will not work. The difference between keydown() and keypress() is that if the user repeats any key by pressing and holding a key, the keydown() event is executed only once whereas the keypress() event isexecuted for each inserted character. Also, the modifier keys Shift, Ctrl, etc. are recognized by keydown(), but keypress() is not fired by these modifier keys.

Read my other post that allows only number

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...