Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

A way to disable the Enter key in javascript?

Posted on 2012-04-06
2
Medium Priority
?
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

596 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