Solved

A way to disable the Enter key in javascript?

Posted on 2012-04-06
2
396 Views
Last Modified: 2012-04-06
Hi,

Is there a way to disable the Enter key in javascript?

I would be OK if it behaved as a Tab key.

Thanks
0
Comment
Question by:taborrg
2 Comments
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 500 total points
ID: 37818131
There you go.
Both solutions. The top form disables enter/return keypresses. The bottom form turns enter/return into TAB (functionally, as in actuality it kills the enter/return, but sends the focus to the next input field).
Notice that you have to be careful not to mix the ids of the input fields.

<html>
<head>
<script type="text/javascript">
	// this will kill the enter/return keys
    function censorship(e){
        var keycode=e.charCode? e.charCode : e.keyCode;
        if (keycode==10 || keycode==13){ // check both RETURN and ENTER
            return false;                // disable keypress
        }
    }
    // this will try to focus on the next input field
    function tabularasa(e,f){
        var keycode=e.charCode? e.charCode : e.keyCode;
        if (type='keypress' && (keycode==10 || keycode==13)){  // check both RETURN and ENTER
            document.getElementById(f).focus();                // move to the specified field
            return false;
        }
    }
</script>
</head>
<body>
<form name="forme1" method="get" action="">
    <label for="field1">Input field 1: <input type="text" name="field1" id="field1" onkeypress="return censorship(event);" ></label><br>
    <label for="field2">Input field 2: <input type="text" name="field2" id="field2" onkeypress="return censorship(event);" ></label><br>
    <label for="field3">Input field 3: <input type="text" name="field3" id="field3" onkeypress="return censorship(event);" ></label><br>
    <input type="submit">
</form>
<hr>
<form name="forme2" method="get" action="">
    <label for="field4">Input field 4: <input type="text" name="field4" id="field4" onkeypress="return tabularasa(event,'field5');" ></label><br>
    <label for="field5">Input field 5: <input type="text" name="field5" id="field5" onkeypress="return tabularasa(event,'field6');" ></label><br>
    <label for="field6">Input field 6: <input type="text" name="field6" id="field6" onkeypress="return tabularasa(event,'field4');" ></label><br>
    <input type="submit">
</form>
</body>

Open in new window

0
 
LVL 1

Author Closing Comment

by:taborrg
ID: 37818154
Awesome.

Works perfectly.

Thanks.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question