There is a simple form

var inp = document.forms[0].send; inp.addEventListener('click', function(e) { e.preventDefault(); console.log('click'); }); 
 <form action="/"> <input type="text" name="text" placeholder="Заполни меня" required> <input type="submit" name="send" value="Отправить"> </form> 

The problem is that when I call
e.preventDefault () - the browser cancels the action of checking for the contents of a tag that has required installed. And even if it is empty - it skips.

That is the question, is it really possible not to cancel this action? Or just write a function to check the contents?

  • one
    Verification is done before shipping. since it didn’t come to the submit, because they disabled the default behavior on the click. then there is no verification, respectively - Grundy
  • exactly, submit works for - user190134

1 answer 1

Verification is carried out before shipping. since it didn’t come to the submit, because they disabled the default behavior on the click. then there is no verification, respectively.

If you migrate e.preventDefault(); into the submit form handler, the check will work.

 var inp = document.forms[0].addEventListener('submit', function(e) { e.preventDefault(); console.log('submit'); }); 
 <form action="/"> <input type="text" name="text" placeholder="Заполни меня" required> <input type="submit" name="send" value="Отправить"> </form> 

  • What is the difference addEventListener and onsubmit = function ? - Mr. Black
  • @Doofy, addEventListener allows several handlers to add - Grundy