Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

I have a survey on a website, and there seem to be some issues with the users hitting enter (I don't know why) and accidentally submitting the survey (form) without clicking the submit button. Is there a way to prevent this?

I'm using HTML, PHP 5.2.9, and jQuery on the survey.

1 Answer

0 votes
by (40.7k points)

Try using the method given below:

$(document).ready(function() {

  $(window).keydown(function(event){

    if(event.keyCode == 13) {

      event.preventDefault();

      return false;

    }

  });

});

Try this to make it more usable and allow people to press Enter if they have completed all the fields:

function validationFunction() {

  $('input').each(function() {

    ...

  }

  if(good) {

    return true;

  }

  return false;

}

$(document).ready(function() {

  $(window).keydown(function(event){

    if( (event.keyCode == 13) && (validationFunction() == false) ) {

      event.preventDefault();

      return false;

    }

  });

});

Related questions

0 votes
1 answer
0 votes
2 answers
asked Sep 2, 2019 in Web Technology by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
asked Aug 28, 2019 in Web Technology by Tech4ever (20.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...