2

I have my website in which I have email pdf functionality

Procedure is :

  • when user enters email and then he has to click on submit button
  • after clicking submit button , form will not submit and form will hide and
  • there is another hidden div contains thank you message which appears with Ok button.
  • When User Click on OK button then form will submit.

But Now the Problem is :

When User enter email and if he press ENTER accidentally then form gets submitted without showing thank you message.

I want to Disable ENTER when user Press Enter key.

j0k
  • 22,600
  • 28
  • 79
  • 90
Surinder ツ
  • 1,778
  • 3
  • 16
  • 27
  • Have you tried this: http://www.w3schools.com/jsref/event_onkeydown.asp – Rob Dec 28 '12 at 13:17
  • @Robuust That's not a good recommendation. The implicit `eval`, the lack of separation of concerns... – John Dvorak Dec 28 '12 at 13:18
  • 1
    [Disable ENTER when user Press Enter key](https://www.google.com/search?q=Disable+ENTER+when+user+Press+Enter+key&oq=Disable+ENTER+when+user+Press+Enter+key&sugexp=chrome,mod=11&sourceid=chrome&ie=UTF-8) – Mr_Green Dec 28 '12 at 13:20
  • Did you do some RnD... see [this](http://stackoverflow.com/questions/2794017/disable-form-submission-via-enter-key-on-only-some-fields) & [this](http://stackoverflow.com/questions/895171/prevent-users-from-submitting-form-by-hitting-enter) & [this](http://stackoverflow.com/questions/11235622/jquery-disable-form-submit-on-enter) – palaѕн Dec 28 '12 at 13:21

5 Answers5

4

Check which key was pressed ant if was the enter key return false. Using jQuery this is easy.

var field = $('.classname');

field.keydown(function(e){
    if(e.which==13){
        return false;
    }
});
Zevi Sternlicht
  • 5,399
  • 19
  • 31
3

you can try this to disable the submit on keypress

$(function() {

    $("form").on("keypress", function(e) {
            if (e.keyCode == 13) return false;
      });

});
Vince Lowe
  • 3,521
  • 7
  • 36
  • 63
1

Use this code, will surely work for you:

var class = $('.classname');
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}

class.onkeypress = stopRKey;
Paz
  • 694
  • 1
  • 5
  • 14
  • I don't like `document.onkeypress = ...`; First, perhaps you shouldn't stop _all_ enter keys. second, this will overwrite any previous event handlers. Use `addEventListener` or jQuery (especially since it's available). – John Dvorak Dec 28 '12 at 13:27
0

Use a regular html button instead of a submit button. In the onclick event of the button write some Javascript to show/hide the DIV. The button on the Thank You DIV make that a submit button.

Kyle Johnson
  • 763
  • 1
  • 13
  • 31
0

Place this in the script:

<script language="JavaScript">
function TriggeredKey(e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    if (window.event.keyCode == 13 ) return false;
}
</script>
ROY Finley
  • 1,406
  • 1
  • 9
  • 18