[RESOLVED]Restricting page redirection on backspace key press

Hi all,

I have content page on one master page. On the same(content page), I have a textbox control which is editable i.e enabled/disabled on ‘ondblclick’ attribute.

Now what I want is ondblclicking, if I press backspace key to erase previous data in textbox it should allow me and  on ‘onblur’, I’m making it readonly/disabled etc. at this time ‘backspace’ key should not work..
also I want to enable ‘delete’ key enabled for editable textbox in ‘onchange’ event by checking keycode(46 in case of delete key).. any suggesion??

My code: 

//as I'm dynamically adding textboxes., .cs code
txt.Attributes.Add("ondblclick", "javascript:SetReadOnlyFalse(this);"); txt.Attributes.Add("onblur", "javascript:SetReadOnlyTrue(this);"); //javascript code: var browser = window.navigator.userAgent; var msie = browser.indexOf("MSIE "); //set read-only attribute to false function SetReadOnlyFalse(textbox) { textbox.removeAttribute("readOnly"); //this is for IE.. we need to force blur and focus the element after double click if ((msie != -1) || (!!navigator.userAgent.match(/Trident/7./))) { textbox.blur(); textbox.focus(); textbox.value.setSelected; } } //set read-only attribute to true function SetReadOnlyTrue(textbox) { textbox.setAttribute("readOnly", "true"); }

any help?? 

Hi sameer.pantvaidya,

First ,I have tested the ondbclicking event which you post ,when you press backspaces ,It will erase the text, and when you onblur the text ,it will not work.

Second,you can not capture the ‘delete’ key press in the onchange event , in the onchange event, the textbox in editable mode,when you

Press Del key, it will input ‘.’ in the textbox ,but You can use keypress event to capture the delete key.

Please try the code below:

 $('#txtId').keypress(function (event) {

                var keycode = (event.keyCode ? event.keyCode : event.which);

                if (keycode == '46') {

                    alert('You pressed a "delete" key in textbox');

                }


            });

Best Regards,

Kevin Shen.

Leave a Reply