January 1, 2013

Disable Enter Key for Submit form excluding multiline textbox

Disable Enter Key hit on submit form excluding TextBox is the hot question now a days. Peoples including me search a lot how to do this. One of my client ask me to disable enter key functionality on form because whenever he hit Enter Key it will allow him to submit form.

In order to complete this task, firstly I search the Enter Key code number which is 13. After that I used these
lines in Header section of page.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
    
    $(document).keypress(function(e) {
    if (e.which == 13 && e.target.tagName != 'TEXTAREA') {            
            return false;
        }
    });    
</script>

Here in above code I use "TEXTAREA", because Asp.Net TextBox having TextMode="MultiLine" will become type TextArea after rendering into HTML.

Using above jQuery code, it will disable enter key hitting on form but it allow multiline TextBox like Asp.net TextBox to hit enter key within it.

1 comment: