/    Sign up×
Community /Pin to ProfileBookmark

form script problem

I’m trying to use the following script to indicate required fields in a form. I get the proper error message when the fields aren’t completed but when I click “OK” in the error message window, I am not returned to the form. Instead, the form goes ahead and completes the “Submit” command, directing me to the “Thanks for Contacting Us” page. Any ideas?

In the <head> section, I inserted:

<SCRIPT LANGUAGE=”JavaScript”>
<!– Original: Wayne Nolting ([email protected]) –>

<!– This script and many more are available free online at –>
<!– The JavaScript Source!! [url]http://javascript.internet.com[/url] –>

<!– Begin
function verify() {
var themessage = “You are required to complete the following fields: “;
if (document.form.name.value==””) {
themessage = themessage + ” – Name”;
}
if (document.form.email.value==””) {
themessage = themessage + ” – E-mail”;
}
//alert if fields are empty and cancel form submit
if (themessage == “You are required to complete the following fields: “) {
document.form.submit();
}
else {
alert(themessage);
return false;
}
}
// End –>
</script>

Here is the form section in the <body>. Let me know if you need more information in order to help. Thanks!

<form name=”form” method=”post” action=”cgi-bin/csvwrite.cgi”>
<tr height=”15″>
<td width=”30″>&nbsp</td>
<td width=”290″>&nbsp</td>
<td width=”290″>&nbsp</td>
</tr>

<tr><td width=”30″>&nbsp</td>
<td colspan=”2″><h6>Name: <input type=”text” name=”name” size=”35″ >*required field</h6></td></tr>

<tr><td width=”30″>&nbsp</td>
<td colspan=”2″><h6>E-mail: <input type=”text” name=”email” size=”35″>*required field</h6></td></tr>

<tr><td width=”30″>&nbsp</td>
<td colspan=”2″><h6>Company Name: <input type=”text” size=”40″ name=”company”></h6></td></tr>

<tr><td width=”30″>&nbsp</td>
<td colspan=”2″><h6>Department: <input type=”text” size=”40″ name=”dept”></h6></td></tr>

<tr><td width=”30″>&nbsp</td>
<td colspan=”2″><h6>Telephone: <input type=”text” size=”25″ name=”phone”></h6></td></tr>

<tr><td width=”30″>&nbsp</td>
<td colspan=”2″><h6>Fax: <input type=”text” size=”25″ name=”fax”></h6></td></tr>

<tr height=”10″><td colspan=”3″>&nbsp</td></tr>

<tr><td width=”30″>&nbsp</td>
<td colspan=”2″><h6>Field(s) of Interest:</h6></td></tr>

<tr><td width=”30″>&nbsp</td>
<td>
<input type=”checkbox” name=”field” value=”water”><span class=”copy”>Water Treatment/Purification</span></td>
<td><input type=”checkbox” name=”field” value=”mining”/><span class=”copy”>Mining</span></td></tr>

<tr><td width=”30″>&nbsp</td>
<td>
<input type=”checkbox” name=”field” value=”chem”><span class=”copy”>Chemicals/Fine Chemicals</span></td>
<td><input type=”checkbox” name=”field” value=”agro”/><span class=”copy”>Agrochemicals</span></td></tr>

<tr><td width=”30″>&nbsp</td>
<td>
<input type=”checkbox” name=”field” value=”pharma”><span class=”copy”>Pharmaceutical/Biopharmaceutical</span></td>
<td><input type=”checkbox” name=”field” value=”food”><span class=”copy”>Food Processing</span></td></tr>

<tr height=”15″><td width=”30″>&nbsp</td>
<td colspan=”2″>
<input type=”checkbox” name=”field” value=”other”><span class=”copy”>Other (please specify) <input type=”text” size=”30″ name=”otherField”></span></td>
</tr>
<tr height=”5″><td colspan=”3″>&nbsp</td></tr>

<tr><td width=”30″>&nbsp</td>
<td colspan=”2″><h6>Question or Comment:</h6><textarea name=”question” rows=”10″ cols=”65″ wrap=”wrap”></textarea></td></tr>
<tr height=”5″><td colspan=”3″>&nbsp</td></tr>

<tr><td class=”center” colspan=”3″>
<input type=”submit” value=”Submit Request” onclick=”verify();”></td></tr>

<tr height=”20″><td colspan=”3″>&nbsp</td></tr>

<tr><td width=”30″>&nbsp</td>
<td colspan=”2″><input type=”checkbox” name=”field” value=”sendEmails”><span class=”copy”>I would like to receive e-mail updates regarding the latest Diazem technology as it relates to my field(s) of interest.</span></td></tr>
</form>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsNov 27.2004 — move verify() from the button to

<form name="form" method="post" onSubmit=" return verify();" action="cgi-bin/csvwrite.cgi">
Copy linkTweet thisAlerts:
@terrieauthorNov 28.2004 — Thanks much - works like a charm!
×

Success!

Help @terrie 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...