Codefixer - ASP tutorials, resources and software
New MS SQL 2008 Available – DiscountASP.NET
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Monday 6 October
MembersPro PayPal - ASP Membership software

USA & Europe Data Centers – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

VBScript Email Validation

Not only is it important to use Javascript client side validation it is also important to use server side (ASP) validation as well. Visitors may have Javascript disabled in their browser so it's very important to use server side validation as a backup. Below is the code for a function 'IsValidEmail' that will check the syntax of an email. Note that the function will only check for the proper syntax it will not check to make sure if the email really exists.

<%
'The IsValidEmail function checks for a valid email
'IsValidEmail returns True if the email address is a valid email
'IsValidEmail returns false if the email address isn't proper syntax
'The code is well commented and should explain what is happening

Function IsValidEmail(emailAddress)
'Declare variables
Dim ValidEmail, emailParts, iLoopCounter, emailChar, acceptableChars, last2Chars
ValidEmail = True 'set the default result to True
'acceptableChars are the characters that we will allow in our email
acceptableChars="abcdefghijklmnopqrstuvwxyz.-_@"
'use the Split function to create an array with the @ as the separator
'so if your email was test@tester.com the email would be split into an array
'with the first array element holding "test" and the second "tester.com"
emailParts = Split(emailAddress, "@")
'check to make sure that there is only 1 @ and that there are 2 parts
'remember arrays are zero based
'Using the UBound function will return the highest element in the array
'So if it's a valid email the UBound function will return 1, i.e. 0 start
If UBound(emailParts) <> 1 Then
ValidEmail = false
Else
'Check the length of each part of the email address
'first part can be just one character, 2nd part must be atleast 4
If Len(emailParts(0))<1 OR Len(emailParts(1))<4 Then
ValidEmail = false
End If
'check first character on the left part isn't a "." using Left function
If Left(emailParts(0), 1)="." Then
ValidEmail = false
End If
'check that there is a . in the second part of the email address - .com
If InStr(emailParts(1), ".") <= 0 Then
ValidEmail = false
End If
'We know there's a . now make sure there are 2 characters after the . for valid email
last2Chars=Right(emailParts(1),2)
If InStr(last2chars,".") Then
ValidEmail = false
End If
'check that there shouldn't be a _ in the second part of the email address
If InStr(emailParts(1), "_") >0 Then
ValidEmail = false
End If
End If
'loop through each character of email
For iLoopCounter = 1 to Len(emailAddress)
'Use Lcase & Mid functions, Mid function used to return each individual character
'in the email, and then Lcase converts it into lowercase
emailChar = Lcase(Mid(emailAddress, iLoopCounter, 1))
'Check if the emailAddress characters are acceptable
If InStr(acceptableChars, emailChar) = 0 and Not IsNumeric(emailChar) Then
ValidEmail = false
End if
Next
'check if there is 2 . in a row
If InStr(emailAddress, "..") > 0 Then
ValidEmail=false
End If
'check if there is @. in a row
If InStr(emailAddress, "@.") > 0 Then
ValidEmail=false
End If
IsValidEmail=ValidEmail
End function
%>

Using the function above here are 2 examples:

<% Response.write IsValidEmail("test@testemail") %>

Output:
false

<% Response.write IsValidEmail("test@testemail.com") %>

Output:
True

You may want to read our tutorials on useful VBScript functions and Arrays to help you understand the code better.

Using Regular Expressions to validate an email.



MembersPro PayPal - ASP Membership software

Membership Software Integrates with PayPal

  • Plug and play Membership script.
  • Recurring subscriptions or one time payment.
  • Up to 3 levels of membership subscription.
  • Easy set up and admin with free installation.
  • Works with PayPal.
  • Receive updates and add-ons for 2 years.
  • 1st class support and customization available.
  • Can be customized to support more levels.
  • Saves you hours and hours of development time and money.
  • PURCHASE YOUR COPY TODAY




If you have any code snippets to share with full credit given then send an email to Codesnippets - You'll receive full credit and a link back to your site.

ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software