PDA

View Full Version : required box in email form


gcapp
10-02-2002, 01:53 PM
One last email question.

Does anyone know the code that I could use if I have a form where I want one box to be a required field (email address to be specific)? And if the person doesn't put their email address in the form, it won't send the form and produce a message saying "email address required".

Thanks,
Gary

BigDaddy
10-02-2002, 02:18 PM
Post the form to itself, and at the top of the form check the email by:


IF email = "" Then
DisplayError()
Else

Thankyou()
End IF

You can also use regular expressions, or instr() to check for the "@" symbol, the ".", etc to verify with reasonable certainty that it is a valid email address.

The DisplayError() function then basically just redisplays the entire page. Next to the email field, you check again:

IF email = "" Then
<font color="red">Email address required</font>
End IF

The Thankyou() function will show if the email address is not blank. If it passes the validation, the thank you function displays. Here you would do your database insert, etc.

dlconnor
10-02-2002, 04:28 PM
Here is a generalized validation routine that should be called from the onsubmit event of the form

function chkNull(theField,lblstr)
{
var temp = parseInt(theField.value.length);
if (temp == 0)
{ alert(lblstr + " is a Required Entry !");
return false; }
else
{ return true; }
}
function validate()
{
var form1 = document.<<your form name>>;

if (!chkNull(form1.email,"EMAIL"))
{ form1.email.focus(); return false; }
return true;
}

gcapp
10-03-2002, 12:22 AM
Well I am new to asp and i'm not sure where to put the validation code.

If BigDaddy or dlconner can show me where to put the validation code in my asp code below, it would be of great help.



I'll just put the email portion of the form below, along with the parts of the asp code that it refers to:

<tr>
<td width="61"><b><font face="Arial" size="1">*Email:</font></b></td>
<td width="389"><font face="Arial" size="2"><input type="text" name="required_email" size="30"><b>(*must
be filled in!!)</b></font></td>
</tr>



If Request.Form("posted") = "True" Then
Strrequired_email = Request.Form("required_email") & " "


'Name of Mail server used for ASPMail
SMTP_SERVER = "mail.enchantedmountains.info" 'alentus

StrALL = StrALL & "Email: " & Strrequired_email & vbcrlf


Hopefully this isn't confusing, I just wanted to put all the parts of the page that has to do with the required email.

If someone can show me where the code goes, I'd appreciate it.

whammy
10-03-2002, 12:26 AM
First of all, I'd follow BigDaddy's advice... why? Because I know personally that he's speaking from experience! :)

As for the email validation, if you're using ASP/VBScript already, then I'd avoid using javascript for stuff like validation...

Wait... even better... look at my example contact form... it has all of that stuff in it (including the fact that it works exactly as BigDaddy is talking about) - feel free to tear it apart:


<% @Language="VBScript" %>
<% Option Explicit %>
<%
' Dimension the form variables
Dim Name, Email, Subject, Message

' Dimension variables used in the application
Dim Pass, Complete, objMail

Function ValidEmail(email) ''''''''''''''''''''''
Dim veRegEx
Set veRegEx = New RegExp
veRegEx.Pattern = "^[\w\.-]+@[\w\.-]+\.[a-zA-Z]{2,}$"
ValidEmail = veRegEx.Test(email)
End Function ''''''''''''''''''''''''''''''''''''

Function RemoveSpaces(str) ''''''''''''''''''''''
If IsNull(str) Then str = ""
Dim rsRegEx
Set rsRegEx = New RegExp
rsRegEx.Pattern = "\s"
rsRegEx.Global = True
RemoveSpaces = rsRegEx.Replace(str,"")
End Function ''''''''''''''''''''''''''''''''''''

' Get our variables from the previous form

Name = Trim(Request.Form("Name"))
Email = Trim(RemoveSpaces(Request.Form("Email")))
Subject = Trim(Request.Form("Subject"))
Message = Trim(Request.Form("Message"))
Pass = Request.Form("Pass")

'''''''''''''''''''''''''''''''''''' MAIN PROGRAM

Pass = Pass + 1

If Name = "" OR ValidEmail(Email) = False OR Subject = "" OR Message = "" Then
DisplayForm()
Else
SendEmail()
End If

'''''''''''''''''''''''''''''''' END MAIN PROGRAM
%>

<% Sub DisplayForm() '''''''''''''''''''''''''''' %>
<html>
<head>
<title>Contact Support</title>
<style type="text/css">
<!--
.error{
color: #ff0000
}
-->
</style>
</head>

<body>

<h1>Contact Support</h1>

<form name="emailform" action="email.asp" method="post">

<table>
<tr>
<td valign="top"<% If Pass > 1 AND Name = "" Then Response.Write(" class=""error""") %>>Name: </td>
<td><input type="text" name="Name" value="<% = Server.HTMLEncode(Name) %>" /></td>
</tr>
<tr>
<td valign="top"<% If Pass > 1 AND ValidEmail(Email) = False Then Response.Write(" class=""error""") %>>Email: </td>
<td><input type="text" name="Email" value="<% = Server.HTMLEncode(Email) %>" /></td>
</tr>
<tr>
<td valign="top"<% If Pass > 1 AND Subject = "" Then Response.Write(" class=""error""") %>>Message Subject: </td>
<td><input type="text" name="Subject" value="<% = Server.HTMLEncode(Subject) %>" /></td>
</tr>
<tr>
<td valign="top"<% If Pass > 1 AND Message = "" Then Response.Write(" class=""error""") %>>Message: </td>
<td><textarea rows="15" cols="20" name="Message"><% = Server.HTMLEncode(Message) %></textarea></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="Pass" value="<% = Pass %>" />
<input type="submit" value="Submit" />
</td>
</tr>
</table>

</form>

</body>
</html>
<% End Sub '''''''''''''''''''''''''''''''''''''' %>

<% Sub SendEmail() ''''''''''''''''''''''''''''''
Set objMail = Server.CreateObject("CDONTS.NewMail")
objMail.To = "support@solidscripts.com"
objMail.From = """" & Name & """" & " <" & Email & ">"
objMail.cc = ""
objMail.bcc = ""
objMail.Subject = Subject
objMail.Body = Message
objMail.Send
Set objMail = Nothing
' We'll insert some HTML below to make this a Thank You page. %>
<html>
<head>
<title>Thank you!</title>
<meta http-equiv="refresh" content="4;URL=email.asp">
</head>

<body>

<h1>Thank you!</h1>

<p>Thank you for contacting us. We will get back to you as soon as possible.</p>

</body>
</html>
<% End Sub '''''''''''''''''''''''''''''''''''''' %>


P.S. To test it out click here (http://www.solidscripts.com/email.asp)

But don't send too many messages to me. Ok? :)