user1953825
user1953825

Reputation:

Is there a way to disable a form from submitting on pressing the "enter" key?

I am creating a simple program in JavaScript for school that randomizes numbers between 1 and a value submitted by the user through a simple form. On submitting the value, I hide the form through jQuery and dynamically append a text prompt with the randomizer result and a reset button in its place.

At this point, I'm having problems getting around the use of the enter key, as the form (as opposed to the reset button) still responds to the press of the enter key, creating havoc, as my reset button is set to trigger on a click. So is there any way to disable the enter key from triggering the submit event?

Upvotes: 0

Views: 432

Answers (3)

Four_lo
Four_lo

Reputation: 1150

if (window.event.keyCode) = 13; return false

to disable the return key

Disable keyboard <enter> key -You will get better answers if you try a few things first, if not just so people can get a better understanding of what are you trying to do

Upvotes: 0

Teneff
Teneff

Reputation: 32148

This is how you can disable form submitting with jQuery:

$('form').submit(function(event){ // on form submisson
    event.preventDefault(); // prevent default action
});

edit: the code in jsFiddle

Upvotes: 0

Sudip
Sudip

Reputation: 2051

Should look like

<form name="" id="" action="" onsubmit="javascript:return false;">
</form>

Upvotes: 1

Related Questions