/    Sign up×
Community /Pin to ProfileBookmark

If theres an error, don’t load the page?

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=”[email protected]“>
<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>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@pyroFeb 06.2003 — Maybe try this instead...

&lt;script language="javascript"&gt;
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&lt;1 || num3&gt;20)
{
alert("Your name must be between 1 and 20 characters'")
return false;
}
return true;
}
&lt;/script&gt;


and this for your form

&lt;FORM name="guess" METHOD="post" ACTION="/cgi-bin/guestbook.cgi" onsubmit="return validate(this)"&gt;

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

&lt;CENTER&gt;
&lt;INPUT TYPE=submit VALUE="SEND"&gt;
&lt;INPUT TYPE=reset VALUE="CLEAR"&gt;
&lt;/CENTER&gt;
&lt;/FORM&gt;
Copy linkTweet thisAlerts:
@gokouauthorFeb 06.2003 — 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?
Copy linkTweet thisAlerts:
@pyroFeb 06.2003 — 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? ?
Copy linkTweet thisAlerts:
@gokouauthorFeb 06.2003 — 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.
×

Success!

Help @gokou spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...