JavaScript Accept only alphanumeric characters

Ensures that an alphanumeric was entered by checking for the presence of the shift, ctrl or alt keys in the event.
keyEvent = function(e) { 
	var code = ('which' in e) ? e.which : e.keyCode;
	if ((code >= 48 && code <= 57 && !e.altKey && !e.shiftKey && !e.ctrlKey) || 
	    (code >= 65 && code <= 90)) {
		// do something
	}
});
Posted by
Snippet Viewed 3807 times.

Share your JavaScript code snippets:

  • Get some recognition & a link back to your site.
  • Create your own code library.
  • Help your fellow developers, as they have helped you.
Submit | Browse

Most Recent javascript snippets

Most Viewed

The FitDesk Under Desk Elliptical