How to block form submission when you press enter or return?

I have a form in which I use jQuery, I donโ€™t think I need to put the code in this post, the question is quite simple.

But I would like to block the form from submitting when people press Enter or Return Key.

At the same time, I have text fields where the user will need to press Enter / Return.

+3
source share
4 answers

A quick and unpleasant hack, but usually more reliable than trying to block keystrokes in each field: add:

<input type="submit" onclick="return false;" />

. , Enter , Enter-.

CSS, / , .

; , .

+4

, submitform = false; . onclick submit.

0

onsubmit , , โ€‹โ€‹ ?

0

jQuery(document).ready(function(){
   validSubmit = false;
})

jQuery('myForm textarea').keypress(function(e) {
   if (e.which == 13) {
      validSubmit = true; // if the pressed key is enter, then allow submissions
   }
})

jQuery('myForm').submit(function(){
   if (!validSubmit) {
      return false;  //if submitting the form is not allowed, then cancel submission
   }
})

, / . , .

jQuery('form button.validSubmit').click(function(){  //or 'form input[type="submit"]'
   validSubmission = true;
})
0
source

Source: https://habr.com/ru/post/1717776/


All Articles