5

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;
    }

}
Ashin Amanulla
  • 59
  • 1
  • 1
  • 4
  • Remove the `return` there. – Obsidian Age Mar 17 '22 at 03:58
  • 1
    @ObsidianAge Thanks for the reply. I have tried removing the return but the form gets submitted even in a false value. – Ashin Amanulla Mar 17 '22 at 04:27
  • 1
    Can you elaborate on what you mean when you say 'submitted even in a false value' – Zach Jensz Mar 17 '22 at 05:24
  • 1
    @ZachJensz As per logic, if the validate function returns false, the form is not supposed to get submitted and redirected to the index.html page. Here, after removing 'return' from the code, onsubmit='validate()' gets submitted and redirected irrespective of returning the value, which is true or false. – Ashin Amanulla Mar 17 '22 at 06:35
  • Are you using preventDefault() to prevent the form from being submitted as per default? Please add your JavaScript to the question so I can help you – Zach Jensz Mar 17 '22 at 06:41
  • @ZachJensz I have just added the codes in the main question. Appreciate your prompt response. – Ashin Amanulla Mar 17 '22 at 09:09
  • Were you using vscode by chance? I think I have the same problem - using `return false;` to prevent form submission and refresh. But behavior suddenly changed for some reason as vscode suddenly says there's an error when it's worked perfectly fine before. – phlaxyr Apr 16 '22 at 22:37

2 Answers2

4

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.

phlaxyr
  • 923
  • 1
  • 8
  • 22
-1

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