ASP.NET Core 2: Disabling Backspace Navigation with Typescript

Today’s Tip:

This bit of Typescript will disable the BackSpace navigation on a page, but allow it on Text Boxes and Password Boxes.

$(document)
    .unbind("keydown")
    .bind("keydown",
        event => {
            var doPrevent = false;
            if (event.keyCode === 8) {
                 const d: any = event.srcElement || event.target;
                if ((d.tagName.toUpperCase() === "INPUT" &&
                        (d.type.toUpperCase() === "TEXT" || d.type.toUpperCase() === "PASSWORD")) ||
                    d.tagName.toUpperCase() === "TEXTAREA") {
                    doPrevent = d.readOnly || d.disabled;
                } else {
                    doPrevent = true;
                }
            }
             if (doPrevent) {
                event.preventDefault();
            }
        });

 

Advertisements

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 )

Google photo

You are commenting using your Google 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