/    Sign up×
Community /Pin to ProfileBookmark

Validation Does Not Work (Stared for Couple Hours)

This Validation will not work. I put some alerts in and have noticed that when I put document.contact.sname.value the alert says undefined. Here is the code thank you.
<form method = “post” name = “contact” onsubmit = “return(checkForm());” action = “<% =home %>mail/contact.asp”>
<table width = “385” cellspacing = “0” cellpadding = “0” align = “left”>
<tr>
<td width = “30%” class = “notCenter”>Name:</td>
<td width = “70%” class = “notCenter”><input type = “text” name = “sname” size = “25”></td></tr>
<tr>
<td width = “30%” class = “notCenter”>Email:</td>
<td width = “70%” class = “notCenter”><input type = “text” name = “semail” size = “25”></td></tr>
<tr>
<td width = “30%” class = “notCenter”>Company:</td>
<td width = “70%” class = “notCenter”><input type = “text” name = “scompany” size = “25”></td></tr>
<tr>
<td width = “30%” class = “notCenter”>Send To:</td>
<td width = “70%” class = “notCenter”>
<select name = “ssend” size = “1”>
<option selected value = “Webmaster”>Webmaster</option>
<option value = “Scott Rhoads”>Commercial Department</option>
<option value = “John Hayden”>Design & Build Department</option>
<option value = “Eric Stickler”>Residential Department</option>
<option value = “Jim Brewster”>Service Department</option>
<option value = “Tom Schauer”>ATC Department</option>
<option value = “Bob Froelich”>Sheet Metal Department</option>
</select></td></tr>
<tr>
<td width = “30%” class = “notCenter”>Subject:</td>
<td width = “70%” class = “notCenter”><input type = “text” name = “ssubject” size = “25”></td></tr>
<tr>
<td width = “30%” class = “notCenter”>Message:</td>
<td width = “70%” class = “notCenter”><textarea name = “smessage” cols = “19” rows = “4”></textarea></td></tr>
<tr>
<td width = “30%”>&nbsp;</td>
<td width = “70$”>&nbsp;</td></tr>
<tr>
<td colspan = “2”><input type = “submit” name = “Submit” value = “Submit”> <input type = “reset” name = “resetIt” value = “Reset”></td></tr>
</table></form>
<script language = “javascript”>
function checkForm()
{
alert(document.contact.sname.value);
if (document.contact.sname.value == “”)
{
alert(“Enter Your Name”);
document.contact.sname.focus();
return false;
}
if (document.contact.semail.value == “”)
{
alert(“Enter Your Email”);
document.contact.semail.focus();
return false;
}
if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(document.contact.semail.value))
{
}
else
{
alert(“Enter a Valid Email”);
document.contact.semail.focus();
return false;
}
if (document.contact.ssubject.value == “”)
{
alert(“Enter the Subject”);
document.contact.ssubject.focus();
return false;
}
if (document.contact.smessage.value == “”)
{
alert(“Enter a Message”);
document.contact.smessage.focus();
return false;
}
alert(“Thanks for Contacting Us”);
return true;
}

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@CharlesJul 06.2004 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

<style type="text/css">

<!--

label {display:block; margin:1em 0em; text-align:right}

input, textarea, select {margin-left:1em}

fieldset {float:left; padding:3em}

-->

</style>

<script type="text/javascript">

<!--

String.prototype.isEmailAddress = function () {return this.match(/^[^x00-x20()<>@,;:".[]x7f-xff]+(?:.[^x00-x20()<>@,;:".[]x7f-xff]+)*@[^x00-x20()<>@,;:".[]x7f-xff]+(?:.[^x00-x20()<>@,;:".[]x7f-xff]+)+$/)}

function checkForm (f) {

if (!/S/.test(f.sname.value)) {alert('Please provide your name.'); f.sname.focus(); return false}

if (!/S/.test(f.semail.value)) {alert('Please provide your email address.'); f.semail.focus(); return false}

if (!/S/.test(f.ssubject.value)) {alert('Please provide a subject for your message.'); f.ssubject.focus(); return false}

if (!/S/.test(f.smessage.value)) {alert('Please provide a message.'); f.smessage.focus(); return false}

}

// -->

</script>

<form action="" onsubmit="return checkForm(this)">

<fieldset>

<legend>Send us a message</legend>

<label>Name:<input type = "text" name = "sname" size = "25"></label>

<label>Email:<input type = "text" name = "semail" size = "25" onchange="if (!this.value.isEmailAddress()) {alert('That would not appear to be a valid email address'); this.value = ''; this.focus()}"></label>

<label>Company:<input type = "text" name = "scompany" size = "25"></label>

<label>Send To:

<select name = "ssend" size = "1">

<option selected value = "Webmaster">Webmaster</option>

<option value = "Scott Rhoads">Commercial Department</option>

<option value = "John Hayden">Design &amp; Build Department</option>

<option value = "Eric Stickler">Residential Department</option>

<option value = "Jim Brewster">Service Department</option>

<option value = "Tom Schauer">ATC Department</option>

<option value = "Bob Froelich">Sheet Metal Department</option>

</select></label>

<label>Subject:<input type = "text" name = "ssubject" size = "25"></label>

<label>Message:<textarea name = "smessage" cols = "19" rows = "4"></textarea></label>

<button type="submit">Submit</button><button type="reset">Reset</button>

</fieldset>

</form>[/font]
Copy linkTweet thisAlerts:
@daub815authorJul 06.2004 — <form method = "post" name = "contact" action="" onsubmit="return checkForm(this)" action = "<% =home %>mail/contact.asp">

<fieldset>

<legend>Send us a message</legend>

<label>Name:<input type = "text" name = "sname" size = "25"></label>

<label>Email:<input type = "text" name = "semail" size = "25" onchange="if (!this.value.isEmailAddress()) {alert('That would not appear to be a valid email address'); this.value = ''; this.focus}"></label>

<label>Company:<input type = "text" name = "scompany" size = "25"></label>

<label>Send To:

<select name = "ssend" size = "1">

<option selected value = "Webmaster">Webmaster</option>

<option value = "Scott Rhoads">Commercial Department</option>

<option value = "John Hayden">Design & Build Department</option>

<option value = "Eric Stickler">Residential Department</option>

<option value = "Jim Brewster">Service Department</option>

<option value = "Tom Schauer">ATC Department</option>

<option value = "Bob Froelich">Sheet Metal Department</option>

</select></label>

<label>Subject:<input type = "text" name = "ssubject" size = "25"></label>

<label>Message:<textarea name = "smessage" cols = "19" rows = "4"></textarea></label>

<button type="submit">Submit</button><button type="reset">Reset</button>

</fieldset>

</form>

Below is in a Separate Script File (which is correctly Linked)

function checkForm(f)

{

alert(document.forms[contact].sname.value);

if (!/S/.test(f.sname.value))

{

alert("Enter Your Name");

f.sname.focus();

return false;

}

if (!/S/.test(f.semail.value))

{

alert("Enter Your Email");

f.semail.focus();

return false;

}

if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(f.semail.value))

{

}

else

{

alert("Enter a Valid Email");

fsemail.focus();

return false;

}

if (!/S/.test(f.ssubject.value))

{

alert("Enter the Subject");

f.ssubject.focus();

return false;

}

if (!/S/.test(f.smessage.value))

{

alert("Enter a Message");

f.smessage.focus();

return false;

}

alert("Thanks for Contacting Us");

}

This is what I have. It is basically what you posted with only a couple changes. It still doesn't work. Thank you
Copy linkTweet thisAlerts:
@CharlesJul 06.2004 — [i]Originally posted by daub815 [/i]

[B]This is what I have. It is basically what you posted with only a couple changes. It still doesn't work. Thank you [/B][/QUOTE]
[font=georgia]Don't make those changes. And be careful, this board often inserts bad spaces ('s') in the posts.

Use my email address validation. It's closer to the email address specification and it alerts the user immediately.

Use[font=monospace]f.sname.value[/font] to get the value of 'sname'. The method you are trying to use is wrong on several accounts.[/font]
Copy linkTweet thisAlerts:
@daub815authorJul 06.2004 — Thanks. I appreciate it the fast response. Thank you.
×

Success!

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