Click to See Complete Forum and Search --> : If theres an error, don't load the page?


gokou
02-05-2003, 10:40 PM
I have this cgi guestbook with a validation form script. What happens is, when you hit the submit button, a page loads and thanks you for your feedback. If you put any errors in the form alerts pop up and the page still loads after you close the alert boxes'. This is also posted in the cgi section, but no one was replying there. Heres the code.

<HTML>
<HEAD>
<TITLE>Guestbook Script</TITLE>

</HEAD>

<BODY BGCOLOR="#FFFFFF">
<!-- VALIDATION FORM SCRIPT -->
<script language="javascript">
function validate()
{
var num=document.guess
var num2=num.email.value.indexOf("@")
var num3=num.name.value
if (num2 == -1)
{
alert("Not a valid e-mail")
submitOK="False"
}
if (num3<1 || num3>20)
{
alert("Your name must be between 1 and 20 characters'")
submitOK="False"
}
if (submitOK=="False")
{
return false
}
}
</script>
<FORM name="guess" METHOD="post" ACTION="/cgi-bin/guestbook.cgi">

<INPUT NAME="name" SIZE=50 TYPE="text"> <B>Your Name</B><BR>
<INPUT NAME="email" SIZE=50 TYPE="text"> <B>Your E-Mail Address</B><BR>
<INPUT TYPE="hidden" NAME="submitaddress" VALUE="brucelee@uogameresources.com">
<p>
<select name="selection">
<option value="pick one" selected>Pick One
<option value="leave feedback">Leave Feedback
<option value="order">Place an Order
<option value="question">Questions'
</select> <b>Choose a subject</b><p>
<b>Write to me below:</b><br>
<TEXTAREA NAME="feedback" ROWS=10 COLS=50></TEXTAREA><P>

<CENTER>
<INPUT TYPE=submit VALUE="SEND" onclick="validate()">
<INPUT TYPE=reset VALUE="CLEAR">
</CENTER>
</FORM>

</BODY>
</HTML>

pyro
02-05-2003, 11:17 PM
Maybe try this instead...

<script language="javascript">
function validate(theForm)
{
var num2=theForm.email.value.indexOf("@")
var num3=theFrom.name.value
if (num2 == -1)
{
alert("Not a valid e-mail")
return false;
}
if (num3<1 || num3>20)
{
alert("Your name must be between 1 and 20 characters'")
return false;
}
return true;
}
</script>

and this for your form

<FORM name="guess" METHOD="post" ACTION="/cgi-bin/guestbook.cgi" onsubmit="return validate(this)">

<INPUT NAME="name" SIZE=50 TYPE="text"> <B>Your Name</B><BR>
<INPUT NAME="email" SIZE=50 TYPE="text"> <B>Your E-Mail Address</B><BR>
<INPUT TYPE="hidden" NAME="submitaddress" VALUE="brucelee@uogameresources.com">
<p>
<select name="selection">
<option value="pick one" selected>Pick One
<option value="leave feedback">Leave Feedback
<option value="order">Place an Order
<option value="question">Questions'
</select> <b>Choose a subject</b><p>
<b>Write to me below:</b><br>
<TEXTAREA NAME="feedback" ROWS=10 COLS=50></TEXTAREA><P>

<CENTER>
<INPUT TYPE=submit VALUE="SEND">
<INPUT TYPE=reset VALUE="CLEAR">
</CENTER>
</FORM>

gokou
02-06-2003, 12:50 AM
Thanks, that helped alot. There is still one little problem though. An alert box will pop up twice for the same error. I didn't change the script exactly how you changed it, only the parts I thought would actuallly change anything.

Heres what I changed in the script:

<script language="javascript">
function validate(guess)
{
var num=document.guess
var num2=num.email.value.indexOf("@")
var num3=num.name.value
if (num2 == -1)
{
alert("Not a valid e-mail")
return false;
}
if (num3<1 || num3>20)
{
alert("Your name must be between 1 and 20 characters")
return false;
}
return true;
}
</script>


and this is what I changed in the form:

<FORM name="guess" METHOD="post" ACTION="/cgi-bin/guestbook.cgi" onsubmit="return validate(this)">

I was wondering what the *this* means inside of the parentheses?

pyro
02-06-2003, 07:14 AM
The "this" returns the name of the form, and then, you call it from your function like this...

function validate(somevar)
//somevar will return the name of the form
var num2=somevar.name.value

Make sense? :)

gokou
02-06-2003, 12:26 PM
yeah, that made sense, thanks'. I figured out why alert boxes were popping up twice. The function was being called for twice. First by the form (onsubmit="return validate(this)), then by the submit button(onclick="validate()). I just deleted the onclick one and now the alert only pops up once.