Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do an alert on keyup only if the key is a letter or number

I would like to show an alert on keyup event but only if the key was a letter or a number, not for shift,tab etc.

<input type='text' id='hi />

or for any key except for tab, shift, ctrl, enter

anybody knows how ?

like image 434
Omu Avatar asked Mar 11 '11 09:03

Omu


People also ask

How do you know if an event key is a number?

We can use isFinite() on event. key , which will check if event. key is a finite number.

What is the difference between Keyup and keypress?

The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress .

Should I use Keyup or Keydown?

Both are used as per the need of your program and as per the convenience of the user. keyup Fires when the user releases a key, after the default action of that key has been performed. keydown Fires when the user depresses a key.


1 Answers

You will have to attach an "keyup" event to the textbox and inside the event check for the keycodes you want:

$("#hi").bind("keyup", function(e) {
       //on letter number
       if (e.which <= 90 && e.which >= 48)
       {
          alert('hello');
       }
});
like image 101
The_Butcher Avatar answered Oct 30 '22 22:10

The_Butcher