/    Sign up×
Community /Pin to ProfileBookmark

Problem with this alert box

Hello, I would really appreciate some help.
I’m using the following code to validate a form. If one of the fields is not filled in or correct, a .gif with a message is displayed under the field that needs to be corrected/filled in. The whole purpose of the code is to do away with the alert box. But I want both an alert box to be displayed and still have the code display the .gif under the field.
How can I implement this?
Thanks in advance for your help.

Here is the code Ive been using

<SCRIPT LANGUAGE=”JavaScript”>
<!– Original: Jeff Harding ([email protected]) –>
<!– Web Site: [url]http://www.site-ations.com[/url] –>
<!– Modified by: Ronnie T. Moore, Editor –>

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

<!– Begin
// Preload images
var empty = new Image(); empty.src = “fieldempty.gif”;
var email = new Image(); email.src = “emailerror.gif”;
var zipcd = new Image(); zipcd.src = “ziperror.gif”;
var phone = new Image(); phone.src = “phoneerror.gif”;

var haveerrors = 0;
function showImage(imagename, imageurl, errors) {
document[imagename].src = imageurl;
if (!haveerrors && errors) haveerrors = errors;
}

function validateForm(f) {
haveerrors = 0;
(f.fname.value.length < 1) // validate first name length
? showImage(“firstnameerror”, “fieldempty.gif”, true) // no semi-colon after this line!
: showImage(“firstnameerror”, “blankimage.gif”, false); // true = errors, false = no errors

(f.lname.value.length < 1) // validate last name length
? showImage(“lastnameerror”, “fieldempty.gif”, true)
: showImage(“lastnameerror”, “blankimage.gif”, false);

(f.zip.value.length != 5) // validate zip code length
? showImage(“ziperror”, “ziperror.gif”, true)
: showImage(“ziperror”, “blankimage.gif”, false);

phonenumlength = f.area.value.length +
f.exchange.value.length + f.number.value.length;

(phonenumlength != 10) // validate phone number length
? showImage(“phoneerror”, “phoneerror.gif”, true)
: showImage(“phoneerror”, “blankimage.gif”, false);

(f.email.value.search(“@”) == -1 || f.email.value.search(“[.*]”) == -1) // validate email
? showImage(“emailerror”, “emailerror.gif”, true)
: showImage(“emailerror”, “blankimage.gif”, false);

return (!haveerrors);
}
// End –>
</script>
</HEAD>
<BODY>
<center>
<form action=”script.cgi” name=”myform” onSubmit=”return validateForm(this)”>
<table border=0 cellspacing=0 celpadding=0>

<tr>
<td colspan=2>Enter your information:<br>
<sup>(<font color=”#ff0000″>*</font> denotes required field).</sup></td>
</tr>
<tr>
<td><p align=right>
First Name:</td>
<td>
<input type=text name=”fname” size=25 maxlength=50><font color=”#ff0000″>*
</font><br>
<img name=firstnameerror src=”blankimage.gif” width=350 height=10 border=0></td>
</tr>
<tr>
<td><p align=right>
Last Name:</td>
<td>
<input type=text name=”lname” size=25 maxlength=50><font color=”#ff0000″>*</font><br>
<img name=lastnameerror src=”blankimage.gif” width=350 height=10 border=0></td>
</tr>

<tr>
<td><p align=right>
Zip Code:</td>
<td>
<input type=text name=zip size=25 maxlength=50><font color=”#ff0000″>*
</font><br>
<img name=ziperror src=”blankimage.gif” width=350 height=10 border=0></td>
</tr>
<tr>
<td><p align=right>
Email:</td>
<td>
<input type=text name=email size=25 maxlength=50><font color=”#ff0000″>*</font><br>
<img name=emailerror src=”blankimage.gif” width=350 height=10 border=0></td>
</tr>
<tr>
<td><p align=right>
Phone:</td>
<td>
<input type=text name=area size=4 maxlength=5>-
<input type=text name=exchange size=4 maxlength=3>-
<input type=text name=number size=5 maxlength=4><font color=”#ff0000″>*
</font><br>
<img name=phoneerror src=”blankimage.gif” width=350 height=10 border=0></td>
</tr>
<tr>
<td colspan=2><p align=center>
<hr>
</td>
</tr>

<tr>
<td><p align=center>
</td>
<td><p align=right>
<input type=submit value=”Submit Form”></td>
</tr>
</table>
</form>
</center>

<p><center>
<font face=”arial, helvetica” size=”-2″>Free JavaScripts provided<br>
by <a href=”http://javascriptsource.com”>The JavaScript Source</a></font>
</center><p>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangFeb 10.2007 — function showImage(imagename, imageurl, errors) {
[COLOR="SeaGreen"]alert(imagename.replace(/error/g,' error'));[/COLOR]
document[imagename].src = imageurl;
if (!haveerrors &amp;&amp; errors) haveerrors = errors;
}
Copy linkTweet thisAlerts:
@stevo55authorFeb 12.2007 — Thanks Fang, works great.
×

Success!

Help @stevo55 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.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

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