Click to See Complete Forum and Search --> : What's wrong with this code???


gcook1
09-14-2005, 09:06 AM
I'm hoping someone can see what I'm doing wrong! I'm writing an ASP web form and I want to do a simple e-mail check. I've stripped it down to just the part that isn't working. I want a pop-up box to prompt the user if he forgets to put in an e-mail address. If he does enter an e-mail address, I just want a simple check to see if the "@" character is in the address. Here's my code...why doesn't it work? The validation isn't happening.

Thanks for your help!

<html>
<head>
<title><%= FORM_NAME %></title>
<link rel="stylesheet" type="text/css" href="forms.css">
<style type="text/css">
<!--
TD {
font-family: Arial;
font-size: 9pt;
}
-->
</style>
</head>

<body <!-- #include file="bodytag.asp" -->>
<!-- #include file="bodyheader2.asp" -->

<script language="VBScript">

dim validation


Function MyForm_OnSubmit
validation = True

If(document.MyForm.strMailTo.Value) = "" Then
MsgBox("An E-mail address is required")
validation = False
Elseif(InStr(1,document.MyForm.strMailTo.Value,"@",1)<2) Then
MsgBox("Your Email address is invalid")
validation = False

End If
If validation = True Then
MyForm_OnSubmit = True
Else
MyForm_OnSubmit = False
End if


End Function

</script>

<%
' setup variables
Dim strLoginID
strLoginID = Replace(Request.ServerVariables("LOGON_USER"), "NA\", "")

Const FORM_NAME = "Seating Incentive Form"


Dim strMailTo, strCopyTo
strMailTo = Request.Form("tbxTo")
strCopyTo = "gcook1@steelcase.com"

If strMailTo <> "" Then ' send email
' format email body
Dim strBody
strBody = "To: " & strMailTo & VbCrLf & _
"Cc: " & strCopyTo & VbCrLf & _
"From: " & strLoginID & "@steelcase.com" & VbCrLf & _
"Date: " & Date & " Time: " & Time & VbCrLf & VbCrLf

' send email
Dim objMail
Set objMail = Server.CreateObject("CDO.Message")
With objMail
.To = strMailTo
.CC = strCopyTo
.From = strLoginID & "@steelcase.com"
.Subject = FORM_NAME
.TextBody = strBody
.Send
End With
Set objMail = Nothing
Response.Redirect "thankyou.asp"
End If
%>

<br><center><h3><%= FORM_NAME %></center></h3>

<!-- this table is used to control format of the page -->
<table width="600" cellpadding="5" cellspacing="0" align="center" style="{border-width: 1px; border-color: gray; border-style: solid }"><tr><td bgcolor="#E5E5E5">

<form name="Form" action="<%= Request.ServerVariables("Script_Name") %>" method="post">

<table>
<tr><td>To:</td><td><input type="text" name="tbxTo" size="30"></td></tr>
<tr><td>Cc:</td><td>Gary Cook</td></tr>
<tr><td>From:</td><td><%= LCase(strLoginID) %>@steelcase.com</td></tr>
<tr><td>Subject:</td><td><%= FORM_NAME %></td></tr>
<tr><td>Date:</td><td><%= Date %></td></tr>
<tr><td>Time:</td><td><%= Time %></td></tr>
</table><br>


<table align="center" width="100%">
<tr>
<td align="center"><input type="reset" value="Clear Form">&nbsp;&nbsp;<input type="submit" value="Submit Form"></td>
</tr>
</table>
</form>

<!-- this table is used to control format of the page -->
</td></tr></table><br><br>
<!-- #include file="footer.asp" -->

</body>
</html>

Bullschmidt
09-14-2005, 05:06 PM
Seems like for client-side scripting one would want to use JavaScript so that non-Microsoft browsers could still use the page.

And here are a few JavaScript validation links:

Javascript Field Validations -- Client Side Scripting by Nannette Thacker - 8/19/1999
http://www.shiningstar.net/articles/articles/javascript/javascriptvalidations.asp?ID=AW

Form Validation Using Javascript - 9/19/1998
http://www.4guysfromrolla.com/webtech/091998-1.shtml
Good tips such as saving JavaScript functions in a file called DataValidation.js and including this in pages that need this.
JavaScript functions.

WebDaily: Your daily source for Web Technology Tips and Tricks! - 10/27/1998
http://www.4guysfromrolla.com/webtech/102798-1.shtml
Just checks if fields filled in.

Or if you want to do some server-side validation with ASP:

Server-Side Form Validation by Dianna Leech - 12/1/1999
http://www.4guysfromrolla.com/webtech/120199-1.shtml

An Email Validation Routine by Joćo Vieira - 4/11/1999
http://www.4guysfromrolla.com/webtech/041199-1.shtml
Somewhat shorter.

An Email Validation Script by Ben Durbin - 5/19/1999
http://www.4guysfromrolla.com/webtech/051999-1.shtml
Longer.