Ahad Ahmed
Ahad Ahmed

Reputation: 49

how to remove the enter key functionality

i am using a form and i have to use button and type is submit when i pressed enter key the form submit, so i want to remove the functionality of enter key.

<input name="bb" id="bb" type="text" onKeyUp="Javascript: if(event.keycode==13) dont_submit(key); else show_error(this.value, event.keycode, this.id)";>


<script type="text/javascript">
function dont_submit(key)
{
    if(key)
    {
    return false;
    }
}

</script>

i also try this but not worked

<script type="text/javascript">
function dont_submit(key)
{

    if(key == 13)
    {
    return false;
    }

}
</script>

Upvotes: 1

Views: 5259

Answers (4)

Irfan TahirKheli
Irfan TahirKheli

Reputation: 3662

$('#button').keypress(function () { 

      if(e.which == 13) {

        return false;
      }
});

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66389

Simply don't put a submit button but rather plain button with JS to submit the form:

<input type="button" class="SubmitButton" value="Submit" onclick="this.form.submit();" />

If you're concerned about visitors without JavaScript, use such code:

<noscript>
    <style type="text/css">
        .SubmitButton { display: none; }
    </style>
    <input type="submit" value="Submit" />
</noscript>

Upvotes: 1

Sasidharan
Sasidharan

Reputation: 3740

$(document).keypress(function (e) {
  if(e.which == 13) 
  return false;
});

Upvotes: 1

Anton
Anton

Reputation: 32581

Try this

$('#bb').on('keydown', function (e) {
    if (e.keyCode == 13) {
        return false;
    }
});

DEMO

Upvotes: 3

Related Questions