473,769 Members | 7,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trapping/preventing keypress in <input> box

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=myK eypressHandler( )" 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
Jul 23 '05 #1
4 10547


owen wrote:
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="ret urn event.keyCode != '\''.charCodeAt ();">

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

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Martin Honnen wrote on 01 dec 2004 in comp.lang.javas cript:


owen wrote:
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="ret urn 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="win dow.status=even t.keyCode;
return event.keyCode != '\''.charCodeAt ();">
Jul 23 '05 #3
owen wrote:
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=myK eypressHandler( )" 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="ret urn
testForApostrop he(event);">
</form>
<script type="text/javascript">
function testForApostrop he(e) { // KEYPRESS event
var k;

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

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.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq

Jul 23 '05 #4


Evertjan. wrote:

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.


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
21301
by: Martin Lucas-Smith | last post by:
I am wanting to know whether are XHTML1-valid characters for use within an id attribute and/or a name attribute. http://validator.w3.org/check?uri=http://www-thaihydro.geog.cam.ac.uk/riverslice/&ss=1&verbose=1 (e.g. see line 77) suggests that it is valid, but the spec suggests that it is not. More detail:
2
5964
by: not 2 swift | last post by:
I thought I would update an old page on which I had used a <INPUT TYPE=image ...> with a <BUTTON><IMG SRC=...></BUTTON> The problem is that <BUTTON> always provides a shadow/emboss effect. Can I get rid of that? If the old <INPUT> button has been deprecated in favor of <BUTTON>, have all it's button functions been duplicated? Thanks for any help,
3
12198
by: iinet | last post by:
How can i set in my css a min width for input elements, but leave the max size dynamic? Ben
3
13156
by: TR | last post by:
Is it possible with CSS to prevent this wrapping alignment with a checkbox with a nested label? This is the label of the checkbox that wraps beneath it I'd prefer it looked like this, with a flush left margin:
5
2259
by: Bart van Deenen | last post by:
Hi all I have a form with a couple of input fields, embedded within spans. I am using script.aculo.us for dragging and dropping, and want to reorder the input fields that way. The input fields are display:inline because I want them all on the same line. Does anyone know of a smart trick to be able to drag these input fields? Just setting their disabled attribute doesn't work, because then they get no events. Not setting disabled just...
4
18435
by: Rick | last post by:
I'm trying to make an input tag visible or hidden based on the value of another input tag, so that when the 1st input tag is changed in anyway, the 2nd input tag will become visible. Thanks in advance, Rick
2
3386
by: Richard Maher | last post by:
Hi, I'm trying to use the Visibility Style attribute for a Div to effectively PopUp a lightweight window with some additional context-sensitive information, when a user mouses over a given field(s). The popping-up seems to work just fine; it's the tearing down that's giving me grief. If I stick a onmouseout event on the same input field that caused the onmouseover/pop-up, it starts to flicker 'cos the <divis placed for esthetically...
2
3289
by: jp2code | last post by:
I have several input fields on my form, and the form works; however, Visual Studio is showing errors, and I would like to get rid of them. When the form is submitted, it is redirected back to itself, where it checks the values that were entered. If the default values are set, this is the visitor's first time to access the page. Example: The FirstName field would initially be displayed with "First Name;"
1
5508
by: test9991014 | last post by:
Hi folks, I've got something like this: <table> <tr> <td>1</td> <td align=center> <input type=text> </td>
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.