How to stop submit from on pressing enter key.


Key code of enter is 13 . so when we need to stop the enter from a text
field we just need to to trace the keypress event and stop for submittion.
Look carefully if condition of stopEnter function–
1> (evt.keyCode == 13) is the checking for enter key is pressed or not
2> (node.type == “text”) is the checking for enter key is pressed on text field or not
3> (node.name == “one”) is the checking for name of field is one or not
<?php    if($_POST){
        echo "Hit Enter";
    }
?>
<html>
    <head>
 <script language="javascript" type="text/javascript">
        function stopEnter(evt) {
            var evt = (evt) ? evt : ((event) ? event : null);
            var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
            if ((evt.keyCode == 13) && (node.type == "text") && (node.name == "one")) {
                return false;
            }
        }
        document.onkeypress = stopEnter;
    </script>

    </head>
    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
            <input type="text" name="one"/>
            <input type="submit" />
        </form>
    </body>
</html>
Advertisements
  1. No trackbacks yet.

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: