Centre for Software Practice

the blog
Welcome to Centre for Software Practice Sign in | Join | Help
in Search

David Glance's Blog

Disabling backspace in forms with Javascript

A constant usability issue has been users losing their data entry on forms by inadvertantly pressing the backspace button when the focus is not in a text box or other appropriate control. As very few users realise that backspace has another function in IE to go back a page, this then gets blamed on the web application as a bug. Of the solutions that are out there, the best I found was one posted here which is reproduced below. We use master pages and so it is just put in the master page.

I have seen some posts from over-zealous accessibility people claiming this is a bad thing - changing default browser behaviour. Since most people believe this default behaviour to be fundamentally broken in the case of web applications, I don't think this is a valid critiscism. 

Here is the BLOCKED SCRIPT

 <script type="text/javascript">

// Trap Backspace(8) and Enter(13) -
// Except bksp on text/textareas, enter on textarea/submit

if (typeof window.event != 'undefined') // IE
  document.onkeydown = function() // IE
    {
    var t=event.srcElement.type;
    var kc=event.keyCode;
    return ((kc != 8 && kc != 13) || ( t == 'text' &&  kc != 13 ) ||
             (t == 'textarea') || ( t == 'submit' &&  kc == 13) ||  (t == 'password'))
    }
else
  document.onkeypress = function(e)  // FireFox/Others
    {
    var t=e.target.type;
    var kc=e.keyCode;
    if ((kc != 8 && kc != 13) || ( t == 'text' &&  kc != 13 ) ||
        (t == 'textarea') || ( t == 'submit' &&  kc == 13) || (t == 'password')) {
        return true
    }
    else {
        return false
    }
   }
</script>

Published Thursday, 1 January 2009 5:02 PM by David.Glance
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems