Click to See Complete Forum and Search --> : Validation Does Not Work (Stared for Couple Hours)


daub815
07-06-2004, 07:44 AM
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;
}

Charles
07-06-2004, 08:06 AM
<!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>

daub815
07-06-2004, 08:20 AM
<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

Charles
07-06-2004, 08:28 AM
Originally posted by daub815
This is what I have. It is basically what you posted with only a couple changes. It still doesn't work. Thank you 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.

Usef.sname.value to get the value of 'sname'. The method you are trying to use is wrong on several accounts.

daub815
07-06-2004, 08:36 AM
Thanks. I appreciate it the fast response. Thank you.