keyCode vs charCode vs Firefox

I am working on a filtered HTML field. The idea is to only allow alphanumeric characters to be typed, and have all other keystrokes ignored.

I am using the keypress event in jQuery to capture the keystrokes – my function returns true for valid characters (which simply passes the event to the field) and false for everything else (which kills the event before it can “reach” the field).

The behavior was fine in IE and Chrome, but not in Firefox. My script was suppressing the arrow keys in only that browser. Certainly, a user should be able to arrow back and forth within the field they are editing.

The problem is twofold. First, different browsers fire the keypress event for some keystrokes, but not all. Chrome and IE didn’t fire the event when using the arrow keys, while Firefox did. Secondly, the keystroke’s value will appear in e.keyCode property in some browsers, but in the e.charCode property in others. Jeez Louise.

This quirksmode article turned out to be a lifesaver. He exhaustively catalogs the differences across browsers, and provides a very valuable little test app (scroll to the bottom of that page).

So, short story long, here is a simple script that I ended up with, I hope it’s useful to you.

$("#username").keypress(function(e) {
	var k = e.keyCode || e.charCode;
	var c = e.charCode;
	var isArrow = (c == 0 && k >= 37 && k <= 40);
	var isSpecial = ((k == 8) || (k == 9) || (k == 127)) || isArrow;	// backspace, tab, delete
	var isOK = (k >= 48 && k <= 57) || (k >= 65 && k <= 90) || (k >= 97 && k <= 122);  // letters and numbers
	return isOK || isSpecial;
});


It hasn’t been exhaustively tested, but is in the “works for me” stage right now. Let me know if it’s helpful.

This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to keyCode vs charCode vs Firefox

  1. A lot of of whatever you assert is astonishingly accurate and it makes me wonder why I had not looked at this in this light previously. This particular piece truly did switch the light on for me personally as far as this particular issue goes. Nonetheless at this time there is just one point I am not really too cozy with so whilst I try to reconcile that with the central theme of your point, allow me observe exactly what the rest of the readers have to say.Very well done.

  2. The heart of your writing whilst appearing agreeable in the beginning, did not really settle well with me after some time. Somewhere within the paragraphs you actually were able to make me a believer but just for a very short while. I still have a problem with your leaps in logic and you would do well to help fill in those breaks. If you actually can accomplish that, I could undoubtedly be amazed.

  3. I do love the manner in which you have presented this specific issue plus it does indeed give us some fodder for thought. On the other hand, from what I have witnessed, I just simply trust as the reviews stack on that men and women keep on issue and in no way get started on a soap box associated with some other news of the day. All the same, thank you for this outstanding piece and although I can not necessarily agree with the idea in totality, I respect your standpoint.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s