I wrote this script for email validation and to send an email. I would appreciate any comments, suggestion, and other feedback. Any suggestions or ideas that are used will be credited in the code. Thanks for your time.
Code:
<%Else
Dim name, email, subject, area_code, phone_prefix, phone_suffix, phoneext, budget, currentsite, pagecount, project
name=request.form("name")
email=request.form("email")
subject=request.form("subject")
area_code=request.form("area_code")
phone_prefix=request.form("phone_prefix")
phone_suffix=request.form("phone_suffix")
phoneext=request.form("phoneext")
budget=request.form("budget")
currentsite=request.form("currentsite")
pagecount=request.form("pagecount")
project=request.form("project")
'Call function to validate form fields
Call validate(name,"req","Name field is required.")
Call validate(email,"req","Email field is required.")
Call validate(email,"email","Please enter valid email (abc@example.com)")
Call validate(area_code,"num","Phone number must be formatted (XXX) xxx-xxxx")
Call validate(area_code,"minlen=3","Phone number must be formatted (XXX) xxx-xxxx")
Call validate(area_code,"maxlen=3","Phone number must be formatted (XXX) xxx-xxxx")
Call validate(phone_prefix,"num","Phone number must be formatted (xxx) XXX-xxxx")
Call validate(phone_prefix,"minlen=3","Phone number must be formatted (xxx) XXX-xxxx")
Call validate(phone_prefix,"maxlen=3","Phone number must be formatted (xxx) XXX-xxxx")
Call validate(phone_suffix,"num","Phone number must be formatted (xxx) xxx-XXXX")
Call validate(phone_suffix,"minlen=4","Phone number must be formatted (xxx) xxx-XXXX")
Call validate(phone_suffix,"maxlen=4","Phone number must be formatted (xxx) xxx-XXXX")
Call validate(budget,"req","Please select project budget.")
Call validate(project,"req","Please enter a project description.")
'end validation
If validated Then
'Compose Body and Call sub function to send email
body="Name: "&name&"<br />Phone: ("&area_code&") " _
&phone_prefix&" - "&phone_suffix&" Ext."&phoneext _
&"<br />Budget: "&budget&"<br />Current Site: "¤tsite& _
"<br />Page Count: "&pagecount&"<br />Project Description:<br />"&project
Call sendEmail("admin@truemediaconcepts.com",body)%>
Validation and send email. (This is the main code)
Code:
<%
'****************************************************************
'* ASP Form Validator *
'* Created by: Luke Howell *
'* True Media Concepts *
'* http://www.TrueMediaConcepts.com *
'* *
'*This code can be used on your site as long as this header *
'*is kept in the code. *
'* *
'*You may not reprint or redistribute this code without *
'*permision from Luke Howell or True Media Concepts. *
'****************************************************************
validated=true
Sub validate(validationValue,validationType,validationMessage)
If Left(validationType,6)="maxlen" Then
splitArray=Split(validationType,"=")
validationType=splitArray(0)
maxnum=CInt(splitArray(1))
End If
If Left(validationType,6)="minlen" Then
splitArray=Split(validationType,"=")
validationType=splitArray(0)
minnum=CInt(splitArray(1))
End If
If Left(validationType,2)="lt" Then
splitArray=Split(validationType,"=")
validationType=splitArray(0)
ltValue=CInt(splitArray(1))
End If
If Left(validationType,2)="gt" Then
splitArray=Split(validationType,"=")
validationType=splitArray(0)
gtValue=CInt(splitArray(1))
End If
If validated Then
Select Case validationType
Case "req"
If validationValue="" Then
Response.Write validationMessage&"<br />"
Response.Write "Click BACK to make the correction.<br />"
validated=false
End If
Case "maxlen"
If len(validationValue)>maxnum Then
Response.Write validationMessage&"<br />"
Response.Write "Click BACK to make the correction.<br />"
validated=false
End If
Case "minlen"
If len(validationValue)>minnum Then
Response.Write validationMessage&"<br />"
Response.Write "Click BACK to make the correction.<br />"
validated=false
End If
Case "alphanum"
If Not isAlphaNumeric(validationValue) Then
Response.Write validationMessage&"<br />"
Response.Write "Click BACK to make the correction.<br />"
validated=false
End If
Case "alpha"
If Not isAlpha(validationValue) Then
Response.Write validationMessage&"<br />"
Response.Write "Click BACK to make the correction.<br />"
validated=false
End If
Case "num"
If Not isNumeric(validationValue) Then
Response.Write validationMessage&"<br />"
Response.Write "Click BACK to make the correction.<br />"
validated=false
End If
Case "email"
If inStr(email,"@")=0 or inStr(email,".")=0 or inStr(email,"@")>inStr(email,".") Then
Response.Write validationMessage&"<br />"
Response.Write "Click BACK to make the correction.<br />"
validated=false
End If
Case "lt"
If validationValue>ltValue Then
Response.Write validationMessage&"<br />"
Response.Write "Click BACK to make the correction.<br />"
validated=false
End If
Case "gt"
If validationValue<gtValue Then
Response.Write validationMessage&"<br />"
Response.Write "Click BACK to make the correction.<br />"
validated=false
End If
End Select
End If
End Sub
Sub sendEmail(toEmail,body)
Dim NewMailObj
'create the mail object and send the details
Set NewMailObj=Server.CreateObject("CDONTS.NewMail")
NewMailObj.From = email
NewMailObj.To = toEmail
NewMailObj.Subject = subject
NewMailObj.Body = body
'you need to add the following lines FOR the mail to be sent in HTML format
NewMailObj.BodyFormat = 0
NewMailObj.MailFormat = 0
NewMailObj.Send
'Close the email object and free up resources
Set NewMailObj = nothing
End Sub
%>