Re-loading / refreshing the page after a JavaScript warning - don't want to!

My JavaScript function works, but for some reason, after a warning appears inside my IF statement, the page reloads / refreshes, and I don't want to do this. Why is this and how can I change my function so that it does not do this?

My function

function valSubmit(){    
    varName = document.form1.txtName.value;
    varSurname = document.form1.txtSurname.value;
    varEmail = document.form1.txtEmail.value;
    varOrg = document.form1.txtOrg.value;

    if (varName == "" || varSurname == "" || varEmail == "" || varOrg == "" ) 
    {

     alert("Please fill in all mandatory fields");  

    }
    else
    { 
        document.body.style.cursor = 'wait';
        document.form1.btnSubmit.style.cursor = 'wait';
        document.form1.action = "http://now.eloqua.com/e/f2.aspx"
        document.form1.submit();
        return true;    
    }

 }

ps I am using ASP.NET 3.5

+3
source share
4 answers

Here is your complete function with the addition of return false.

In addition, when you call valSubmit, it should look like this:

... onsubmit = "return valSubmit ();" ...

Note that you also need to specify return.

Here is the function:

function valSubmit(){

varName = document.form1.txtName.value;
varSurname = document.form1.txtSurname.value;
varEmail = document.form1.txtEmail.value;
varOrg = document.form1.txtOrg.value;

 if (varName == "" || varSurname == "" || varEmail == "" || varOrg == "" ) 
 {

     alert("Please fill in all mandatory fields");
     return false;

 }
 else
 { 
    document.body.style.cursor = 'wait';
    document.form1.btnSubmit.style.cursor = 'wait';
    document.form1.action = "http://now.eloqua.com/e/f2.aspx"
    document.form1.submit();
    return true;    
 }

}
+8
source

return false;, submit

+1

I think you should add return false;after yours alertbecause the form is submitted.

0
source

Just the declaration returns false after the warning. How:

alert("Please fill in all mandatory fields");
return false;
0
source

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


All Articles