Pressing ENTER Submits form, can I stop this?

jmills6 15 Dec, 2009
Well, my subject basically said it all. I've created a form with my own code. Everything about it works beautifully! The only issue I am having is that while the end user is filling out the form, if they accidentily hit ENTER, it submits the form. I have done this while I've been typing, hitting ENTER instead of TAB to progress. Is there a way to stop the form from being submitted on an ENTER keystroke? I understand that if someone has tabbed their way to the submit button, then ENTER would be a logical way to hit that button, but outside of that... eeek! Anyone have any ideas? Have I done something wrong in the coding?
GreyHead 15 Dec, 2009
Hi jmills6,

I can only suggest you Google the problem; there are a few scripts around to prevent this.

Bob
mypethamster 15 Dec, 2009
why not make the input boxes, etc required, that would prevent the pressing of 'Enter' key submitting the form, until all the required input fields were filled.

otherwise use javascript:

the code below works, (might want to remove alert message.
window.onload = function()
	{ //Wait for the page to load.
    var inputs = document.getElementsByTagName('input');
    for(var i=0;i<inputs.length;i++)
		{ 
		if(inputs[i].getAttribute('type')!="submit")
			{
    		 if(window.event)
				{
				inputs[i].onkeypress = function() { keypressed(event)};
				}
			else
				{
				inputs[i].setAttribute('onkeypress', 'keypressed(event)');
				}	 
			}
		 }
	}


function keypressed(e)
{ 
 e = e? e : window.event;
var Key = e.keyCode? e.keyCode : e.which? e.which : null;
if (Key == 13){
alert("Enter button Pressed - Press the damn 'Submit Now' button Fool!")
if(e.preventDefault)
e.preventDefault(); 
e.returnValue=false;  //Prevent form being submitting IE
e.cancel = true;  //Prevent form being submitting other browsers
};
}
jmills6 16 Dec, 2009
THANKS! The java script worked great!
This topic is locked and no more replies can be posted.