Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Crossbrowser prevention of enter key form submission?

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Hello :)

Does anyone know a good crossbrowser script that prevents forms from being submitted by hitting enter key?

What I need is a script that makes it unnecessary to add javascript to form elements.

I've used this one but it doesn't seem to work anymore for some unknown reason :

Code:
var fnav = window.Event ? true : false;

    if (fnav) {
    
    window.captureEvents(Event.KEYDOWN);
    window.onkeydown = NetscapeEventHandler_KeyDown;
    
    } else {
    
    document.onkeydown = MicrosoftEventHandler_KeyDown;
    
    }

function NetscapeEventHandler_KeyDown(e) {

  if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { return false };
  
  return true;

}

function MicrosoftEventHandler_KeyDown() {

  if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit')

return false;
return true;

}

Thanks a lot! :)

 
might not be the best way, but here's one way that works in FF/IE and Chrome:

Code:
<script language="javascript">
window.onload = function() {
	document.getElementById("frm").onsubmit = function() { return false; }
	document.getElementById("btnSubmit").onclick = function() { document.frm.submit(); }
}
</script>
<form action="mypage.html" name="frm" id="frm" method="get">
	<input type="text" name="tbx" id="tbx" /><br />
	<script language="javascript">
		document.write("<input type=\"button\" name=\"btnSubmit\" id=\"btnSubmit\" value=\"submit\" />");
	</script>
	<noscript>
		<input type="submit" name="btnSubmit2" id="btnSubmit2" value="Submit" />
	</noscript>
</form>

Basically, you're telling the browser to return false if the form is attempted to be submitted (i.e. enter key), but when the button is clicked force the submit - highlighting the submit button and clicking enter won't work either.

I wrote the button using JS and included the <noscript> tag so that the form is usable for browsers that have JS turned off....



--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
Thanks Vic but I'm looking for a solution that won't necessite the modification of already existing forms.

I have lots of forms always with different names and the function I've been using so far was great for that.

Actually, I think that the issue is with browser detection because it's "window.captureEvents" that triggers errors in IE.

If I replace
Code:
if (fnav) {
by
Code:
if (navigator.appName != 'Microsoft Internet Explorer') {
then the function works with IE, FF and Safari but not with Opera anymore.

I know that navigator.appName isn't a bullet proof solution but it seems to work.

Anyway, I'm open to any other solution that is as handy as the one I was using before.

Thanks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top