Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

533
Visualizações
'Error: A 'return' statement can only be used within a function body' - HTML Form

I am getting an error in my old HTML form onsubmit return statement. It was working perfectly fine before but recently the same statement is showing an error. I am attaching the screenshot of the error HTML form return statement error

<form action="index.html" onsubmit = 'return validate();'>

  • Error: A 'return' statement can only be used within a function body.*

I have tried removing 'return' but the form gets submitted even if the validate function returns false.

Then I added preventDefault(), but now the form doesn't get submitted or redirected even if the validate function returns true. <form action="index.html" onsubmit = ' preventDefault() validate();'>

JS code

function validate() {
    
    if (em.value.trim() == "" || pas.value.trim() == "") {
        alert("Fields not filled");
        em.style.border = "2px solid red";
        pas.style.border = "2px solid red";

        return false;
    }

    else if (regexp.test(em.value)) {
        er.innerHTML = "VALID!";
        er.style.color = "green";
        return true;
    }
    else {
        er.innerHTML = "INVALID E-MAIL!!"; //!Alert this is important line
        er.style.color = "red";
        em.style.border = "2px solid red";
        return false;
    }

}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If I'm understanding you correctly, you are referring to a trick to use return false; to prevent a form from submitting. See here and here.

As of February 16, it seems that vscode has been throwing an error on the return false;. However, the code still works, even if vscode says there is an error. Therefore it might be best just to ignore vscode's error message.

There is a github issue currently open on vscode mentioning the new behavior change. The github issue also has a guide on disabling errors with vscode. Specifically, I followed tjx666's comment to disable the error squiggles.

over 4 years ago · Santiago Trujillo Relatório

0

Just remove the return for example: onsubmit="verif();" in your case: onsubmit = 'validate();'

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda