You haven't explained what the problem is. Assuming you only want to reload the page if one of the conditions in your
message
function is met, then you'll need to return a value from that function indicating whether a condition was met, and use an
if
block to conditionally execute the
reload
function.
You should also look at the
trim[
^] function to simplify your tests, rather than testing for multiple variations of empty strings and single spaces.
function message(){
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
if (name === '' && email === ''){
alert("Please enter your name and email address.");
return false;
}
if (name === ''){
alert("Please enter your name.");
return false;
}
if (email === ''){
alert("Please enter your email address.");
return false;
}
alert("Thanks for your submission " + name + "!");
return true;
}
<button type="button" onclick="if (message()) { reload(); }">Submit Survey</button>