Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

detecting keystrokes using javascript

Status
Not open for further replies.

y2k1981

Programmer
Aug 2, 2002
773
IE
Hi All:

I have two questions here, both related to the above topic. For the first one, I have an input field where I only want the user to be able to enter digits 0-9 and a "-" (but that could be left out if necessary). What I'd love is somebody to respond back and tell me that there such a thing as an input field type="number" ... but that ain't gonna happen, is it!! Do basicly I want to use onKeyDown to detect if the keystroke is a digit or a "-", if it is then it's ok to proceeed, but otherwise don't key it.

second question: I have an
Code:
onkeyUp="if.this.value.length==4) formname.nextfield.focus()
in one of my input fields, you can see yourself what it does but I don't want this to be called if the key in question is the tab key. if the user is at the field before this field, and tabs to it because they want to change it, it'll automatically go to the next field because the value length is 4. likewise, if they were to use SHIFT + TAB to tab back to it (because they done a typo) it'd keep giving focus to the nextfield also. The only way they would get around this would be to use the mouse to click in the field. so basicly I need to be able to detect if they used the tab key to get here.

thanks in advance for anybody that can help me
Martin
 
Hi
Answer to question the first...

faq216-2818

Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) && (((parseInt(Math.abs(x).toString()+Math.abs(y).toString())-Math.abs(x)-Math.abs(y))%9)!=0)) {alert(&quot;I'm a monkey's uncle&quot;);}
 
Y2K1981, Try this:

<html><head><title>KeyCodes</title></head>
<body onkeydown=&quot;alert('The keyCode is ' + event.keyCode)&quot;>
Press a key
</body></html>

Clive
 
CliveC .. absolutely Brillant!! The keyCode for tab is 9 and for shift is 16 (because you just never know when that might come up on Who Wants to be a Millionaire!!)

I included shift because I regularly use SHIFT + TAB to go back a field -- I'm a keyboard diehard, I'd disconnect the mouse if I was allowed!! Anyway, if you want to see it in action go to -- and if you do visit, let me know what you think of the site. The JS error you get on every page is not my fault!! It's the code for the banner adds that GeoCities puts on so there's nothing I can do!!

Anyway, thanks again. Oh, nearly forgot, thanks HellTel for all your help recently too.

better go
Martin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top