Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent form submit, when focus is on specific input?

How I can prevent form submit in JS/jQuery when one field has a focus on it, while retaining normal behaviour on all other input fields?

like image 425
canni Avatar asked Feb 04 '26 17:02

canni


2 Answers

Try this:

$('#formID').on('submit', function(){
    if ($('input:focus').length){return false;}
});

This is assuming you try to submit the form without click. Because when you click to submit, the input has a blur event and looses focus.

Else: then jcubic's answer might be what you need.

like image 56
Sergio Avatar answered Feb 06 '26 06:02

Sergio


You can use this:

var prevent = false;
$('input').focus(function() {
   prevent = true;
}).blur(function() {
   prevent = false;
});

$('form').submit(function() {
   if (prevent) {
      return false;
   }
});
like image 38
jcubic Avatar answered Feb 06 '26 06:02

jcubic