Checking a form onSubmit

admin

Administrator
Staff member
I have this page that has a form in it. There are only three parts to it: an input area for Name, one for Email and a textarea for Question. It's sort of an ask-a-question page.

Upon submission (onSubmit), the info is sent to my mailbox using CGI coding and the user is referred to a thank you page of some sort.

However, I want to add a feature using JS to check the form. If the Email section and/or the Question section is left empty, when the person submits the form, there will be an alert and then the person will NOT be forwarded to the thank you page. I have used this code:
_________________________________________

<!-- JS PART -->

<script language="JavaScript" type="text/javascript">
//<![CDATA[
function checkform() {

var emailspace = document.askqform.email.value;
questionspace = document.askqform.question.value;

if (emailspace == "") {
window.alert("Please fill in your email address. \n\nWithout it we will not be able to reply to your question!");
return false;
}

if (questionspace =="") {
window.alert("Please fill in the field for your question.");
return false;
}

else {
return true;
}

}
//]]>
</script>
..........

<!-- FORM PART -->

<!-- START FORM -->

<form name="askqform" onSubmit="checkform()" method="post" action="/cgi-bin/askq.cgi">

<b>Your Name</b>: <input name="name" size="55" type="text" /> <br /><br />
<b>Your E-mail Address: <input name="email" size="55" type="text" /> <br /><br />

<input type="hidden" name="hqadd" value=http://www.webdeveloper.com/forum/archive/index.php/"[email protected]" />

<b>Your Question</b>: <br />
<textarea name="question" rows="10" cols="50"></textarea> <br /><br />

<div style="text-align:center">
<input type="submit" value="Submit question" />
<input type="reset" value="Clear fields" />
</div>

</form>
_________________________________________

I did a few tests with it and found that it has a problem. I tried leaving either Email or Question or both blank and submit the form. The alert popped up but it seems that the "return false;" part didn't work. I was still forwarded to the thank you page. That's not what I want.

Is there something wrong with my code or is that something else I need to add?

Thanks a lot in advance.
 
Back
Top