CODE to prevent submission via return key not always working

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

CODE to prevent submission via return key not always working

Post by rhecker »

I do not know javascript at all, but I've been using the follwing code to prevent users from accidently submiting forms by hitting the return key. I have discovered that this works for text fields, but not checkboxes and radioboxes. If they hit enter from a radiobox, the form submits. If someone has a code snippet they can share that REALLY prevents use of the return key, I'd appreciate it.

Code: Select all

<script type="text/javascript"> 
function stopRKey(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"))  {return false;} 
} 
document.onkeypress = stopRKey; 
</script>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: CODE to prevent submission via return key not always working

Post by AbraCadaver »

Because you're checking if the input type was text:

Code: Select all

if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
Try this to see if the input type is not submit:

Code: Select all

if ((evt.keyCode == 13) && (node.type!="submit"))  {return false;}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: CODE to prevent submission via return key not always working

Post by rhecker »

Just gave that a quick test and it worked perfectly. Thanks!
Post Reply