aagjalpankaj
aagjalpankaj

Reputation: 1180

Avoid only form submission on Enter key press

I've one form in that having some inputs as well as textarea like below.

<form id="my_form" action="/action_page.php">
  First name:<br>
  <input type="text" name="firstname">
  <br>
  Last name:<br>
  <input type="text" name="lastname">
  <br>
  Note:<br>
  <textarea id="note" name="note"></textarea>
  <br>
  <input type="submit" value="Submit">
</form> 

I've to disable form submission on pressing Enter key. Form should be submitted only on click of submit button.

I found this solution on stackoverflow, so I wrote below code.

$("#my_form").keypress( function( e ) {
    var code = e.keyCode || e.which;
    if ( code == 13 ) {
        e.preventDefault();
        return false;
    }
});

It's working fine but not allowing newline to be entered in the textarea on pressing Enter key. How do I achieve this?

Upvotes: 2

Views: 1086

Answers (5)

ellipsis
ellipsis

Reputation: 12152

This will work:

<html lang="en">
<head>
  <script
      src="https://code.jquery.com/jquery-3.3.1.min.js"
      integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
      crossorigin="anonymous"></script>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
<form id="my_form" action="/action_page.php">
  First name:<br>
  <input type="text" name="firstname" id="f">
  <br>
  Last name:<br>
  <input type="text" name="lastname" id="l">
  <br>
  Note:<br>
  <textarea id="note" name="note"></textarea>
  <br>
  <input type="submit" value="Submit">
</form>
<script>

  $("#note").keypress( function( e ) {
    var code = e.keyCode || e.which;
    if ( code == 13 ) {

      return true;
    }
  });
  $("#f").keypress( function( e ) {
    var code = e.keyCode || e.which;
    if ( code == 13 ) {
      e.preventDefault();
      return false;
    }

  });
  $("#l").keypress( function( e ) {
    var code = e.keyCode || e.which;
    if ( code == 13 ) {
      e.preventDefault();
      return false;
    }

  });

</script>
</body>
</html>

Give ids to first name and last name input. Disable keypress on them.

Upvotes: 1

Shailesh Rathod
Shailesh Rathod

Reputation: 157

Replace your button with this button

<button type="button" onclick="formSubmit()">Submit</button>

then handler submit event with javascript like below.

function formSubmit() { 
    //here your code
} 

Upvotes: 2

Jay
Jay

Reputation: 373

Easy, just change:

<input type="submit" value="Submit">

To:

<input type="button" value="Submit">

Then handle click of button in jQuery (or pure JS).

Upvotes: 2

Jitendra G2
Jitendra G2

Reputation: 1206

Just replace the $("#my_form") to $("#my_form input:text").

So code will looks like,

$("#my_form input:text").keypress( function( e ) {
    //Do stuff here
});

Upvotes: 0

4b0
4b0

Reputation: 22323

You can use :not() Selector for input.

$("#my_form:not(input:text)").keypress( function( e ) {
    var code = e.keyCode || e.which;
    if ( code == 13 ) {
        e.preventDefault();
        return false;
    }
});

Upvotes: 0

Related Questions