/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Form Validator Not Working Properly

I tried to cobble together some code from other sources before posting, and the code is close to working, but not quite there.

I have a contact form and I want to validate 3 of the 4 fields before it can be submitted, just to make sure they’re not empty.

Here is the form:

[CODE]<form name=”consult” action=”contact.php” method=”post” onsubmit=”return check()”>
<p><label>Name</label><input name=”Name” type=”text” id=”name” /></p>
<p><label>E-mail</label><input name=”E-mail” type=”text” id=”email” /></p>
<p><label>Phone</label><input name=”Phone” type=”text” id=”phone” /></p>
<p><label>Website</label><input name=”Company” type=”text” id=”website” /></p>
<p><button type=”submit” onclick=”check()”>Submit Request</button></p>
</form>[/CODE]

And here is the validation code I’m trying to use:

[CODE]<script language=”javascript”>
function check() {
if (document.consult.Name.value==””) {
alert(“You did not fill our your name.”);
return false;
}
else if (document.consult.E-mail.value==””) {
alert(“You did not fill our your e-mail.”);
return false;
}
else if (document.consult.Phone.value==””) {
alert(“You did not fill our your phone.”);
return false;
} else {
return true;
}
}
</script>[/CODE]

If I leave the form blank and click Submit, I get two alerts in a row that both say “You did not fill out your name”, and that’s it. If I enter something in the “Name” field, the form submits even if the other fields are blank.

I’m sure it’s something simple I’m overlooking, but I don’t know what. Would greatly appreciate any help..

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@tivrfoaApr 03.2009 — Hi!

It's get two alerts in a row, because you call the method twice ?.

Do it: [code=html]<p><input type="submit" value="Submit Request" />[/code]
You can't use E-mail, use Email. That's what causing the problem.

Mark the thread as solved and ident your code next time :mad:
Copy linkTweet thisAlerts:
@dogfighterauthorApr 03.2009 — 

Mark the thread as solved and ident your code next time :mad:[/QUOTE]


Sorry about that, newbie mistake ?

Thanks so much for the help! ?
×

Success!

Help @dogfighter 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.18,
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,
)...