PDA

View Full Version : Disable backspace on form pages


sad69
05-06-2004, 08:12 PM
I've been getting some complaints from my users: when filling out forms, while on a select box, pressing the backspace key would actually mimick pressing the back button (like history.go(-1) ...)

Then I realized that if the user wasn't in a textbox or a textarea, pressing the backspace key would send them back one page. Now I've got a multi-page form in an iFrame and that's really screwing their inputs up as it loses all the information entered in that form (pressing back doesn't submit/post backwards... I wish it would!).

So anyway, here's the script I'm using on all my pages so that the user won't 'accidentally' go back a page. (In my opinion, with my users anyway, a user shouldn't need to hit the backspace unless they're in a textbox or a textarea).


//so backspace doesn't go back
document.onkeydown = checkForBackspace;
function checkForBackspace() {
//we can backspace in a textbox
if(window.event.srcElement.type.match("text")) {
return true;
}

if(window.event && window.event.keyCode == 8) {
// try to cancel the backspace
window.event.cancelBubble = true;
window.event.returnValue = false;
return false;
}
}


This is IE only. srcElement is an IE thing.. I believe most other browsers use target. I don't know what else would be different, but anyway, that's what I'm using.

BTW: If you're wondering why anyone would press backspace in a selectbox, well I recently showed them that you can press the first letter of an item in the list to quickly jump to that item. Well, perhaps naturally, if they press the wrong key, instead of pressing the right key, they try to hit backspace first. Just if you were curious... I guess checkboxes and radios could be similar?

Hope that helps somebody!
Sadiq.

glenngv
05-07-2004, 05:17 AM
The backspace behavior is an IE thing only, so you may want to assign the onkeydown handler for IE only.

if (document.all) document.onkeydown = checkForBackspace;

sad69
05-08-2004, 12:11 AM
Hmm.. good point glenngv. Thanks for that.

Sadiq.

joh6nn
05-08-2004, 11:31 PM
The backspace behavior is an IE thing only

no it's not; i use it in Firefox all the time. i'm pretty sure it's present in Opera, as well, though i'd have to double check that, as i use it only very infrequently