trapping/preventing keypress in <input> box

O

owen

I have an <input> box and i want to disable the apostrophe ( ' ) key, so
when you press it, no character appears in the input box. All other keys
should work ok.

I can trap the keypress event using "onkeypress=myKeypressHandler()" but,
beyond that, I'm stuck. I forget how to detect what key was pressed or how
to "null it out".

I'm using IE6 and users will be IE5.0 upward ONLY (trust me on this, suffice
to say it's not a website but an intranet application).

Any ideas please?

Thanks!
Owen
 
M

Martin Honnen

owen said:
I have an <input> box and i want to disable the apostrophe ( ' ) key, so
when you press it, no character appears in the input box. All other keys
should work ok.

<input type="text"
onkeypress="return event.keyCode != '\''.charCodeAt();">

But note that the right arrow key -> has the same keyCode as the '
character so you will cancel that key too.
 
G

Grant Wagner

owen said:
I have an <input> box and i want to disable the apostrophe ( ' ) key, so
when you press it, no character appears in the input box. All other keys
should work ok.

I can trap the keypress event using "onkeypress=myKeypressHandler()" but,
beyond that, I'm stuck. I forget how to detect what key was pressed or how
to "null it out".

I'm using IE6 and users will be IE5.0 upward ONLY (trust me on this, suffice
to say it's not a website but an intranet application).

Any ideas please?

Thanks!
Owen

<form>
<input type="text" name="yourInput" onkeypress="return
testForApostrophe(event);">
</form>
<script type="text/javascript">
function testForApostrophe(e) { // KEYPRESS event
var k;

if (e && e.which) { // NS
k = e.which;
} else if (window.event && window.event.keyCode) { // IE
k = window.event.keyCode;
}

return (!k || k != 39);
}
</script>

Although your users may wonder if there is something wrong with their keyboard
when they hit an apostrophe and nothing appears.
 
E

Evertjan.

Martin Honnen wrote on 01 dec 2004 in comp.lang.javascript:
<input type="text"
onkeypress="return event.keyCode != '\''.charCodeAt();">

But note that the right arrow key -> has the same keyCode as the '
character so you will cancel that key too.

Not overhere, here the arrow gives no event.keycode at all.


<input type="text"
onkeypress="window.status=event.keyCode;
return event.keyCode != '\''.charCodeAt();">
 
M

Martin Honnen

Evertjan. wrote:

Not overhere, here the arrow gives no event.keycode at all.

Right, I forgot that IE has its very own way of firing the different key
events for such keys, it fires no keypress but only a keydown for the
arrow keys so an onkeypress handler doesn't fire for arrow keys and
therefore doesn't run the risk of cancelling those.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top