Reply LinkBack Thread Tools Search this Thread
Old 06-08-2006, 02:31   #1 (permalink)
snumb130
Registered User
 
Join Date: Aug 2006
Posts: 2
Send a message via AIM to snumb130
opinions or asp email form validation script

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 for form.

Code:
<form id="quote" action="email_quote.asp" method="post"> <table style="width:450px;"> <tr><td style="width:35%;">*Contact Name:</td><td align="right"><input id="name" name="name" size="30" onfocus="this.style.background='#FFFBCF'" onblur="this.style.background='#FFFFFF'" /></td></tr> <tr><td>*Email:</td><td align="right"><input name="email" size="30" onfocus="this.style.background='#FFFBCF'" onblur="this.style.background='#FFFFFF'" /></td></tr> <tr><td>*Phone:</td><td align="right"><span style="font-size:12pt;">(</span><input name="area_code" size="3" maxlength="3" onfocus="this.style.background='#FFFBCF'" onblur="this.style.background='#FFFFFF'" /><span style="font-size:12pt;">)</span>&nbsp;&nbsp;&nbsp;<input name="phone_prefix" size="3" maxlength="3" onfocus="this.style.background='#FFFBCF'" onblur="this.style.background='#FFFFFF'" /><span style="font-size:12pt;"> - </span><input name="phone_suffix" size="4" maxlength="4" onfocus="this.style.background='#FFFBCF'" onblur="this.style.background='#FFFFFF'" />&nbsp;&nbsp;&nbsp;Ext.<input id="phoneext" size="4" maxlength="10" onfocus="this.style.background='#FFFBCF'" onblur="this.style.background='#FFFFFF'" /></td></tr> <tr><td>*Project Budget:</td><td align="right"><select name="budget" onfocus="this.style.background='#FFFBCF'" onblur="this.style.background='#FFFFFF'"> <option value="">Choose Budget</option> <option value="$500 or less">$500 or less</option> <option value="$501 - $1500">$501 - 1500</option> <option value="$1501 - $2500">$1501 - $2500</option> <option value="$2501 - $5000">$2501 - $5000</option> <option value="$5001 - $7500">$5001 - $7500</option> <option value="$7501 - $10000">$7501 - $10000</option> <option value="$10001 or more">$10000 or more</option></select></td></tr> <tr><td>Current Site (if any):</td><td align="right"><input name="curentsite" size="30" value="http://www." onfocus="this.style.background='#FFFBCF'" onblur="this.style.background='#FFFFFF'" /></td></tr> <tr><td>Approximately how many pages do you need:</td><td align="right"><input name="pagecount" size="30" onfocus="this.style.background='#FFFBCF'" onblur="this.style.background='#FFFFFF'" /></td></tr> <tr style="height:5px;"><td colspan="2"></td></tr> <tr><td colspan="2">*Project Description:</td></tr> <tr><td align="right" colspan="2"><textarea name="project" rows="5" cols="45" onfocus="this.style.background='#FFFBCF'" onblur="this.style.background='#FFFFFF'"></textarea></td></tr> <tr><td></td><td><input type="hidden" name="subject" value="True Media Concepts - Quote" /> <input type="submit" value="Send Quote Request" /><input type="reset" value="Reset" /></td></tr> </table> </form>

Page to receive form info.

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:&nbsp;"&name&"<br />Phone:&nbsp;("&area_code&")&nbsp;" _ &phone_prefix&"&nbsp;-&nbsp;"&phone_suffix&"&nbsp;Ext."&phoneext _ &"<br />Budget:&nbsp;"&budget&"<br />Current Site:&nbsp;"&currentsite& _ "<br />Page Count:&nbsp;"&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 %>
  Reply With Quote
Old 06-08-2006, 06:16   #2 (permalink)
paulanthony
mingin dawg baitch
 
paulanthony's Avatar
 
Join Date: Apr 2004
Location: Belfast
Posts: 1,041
Send a message via MSN to paulanthony
You might want to Response.End your validation message. It will at the minute write it to the same page the form is on - and stick the message right at the top - better to set a variable somewhere in the page, and if true show the validation message. You could do the same concept with Javascript to show your message
<% If val = True %>
<script>alert('blah');</script>
<% End If %>

CDONTS is out of date - use CDO. Your sendmail function cannot be reused - due to the fact that the variables are not all passed in the arguments. sendEmail(from, to, subject, body) would be a better set of arguments.

Sub validate should be a function returning a value. Not a sub. Again think reusable code.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8