Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP : Server Side Email Addresses Validation using VBScript
 

Server Side Email Addresses Validation using VBScript

by .

Introduction :
In this tutorial I intend to teach you how to take a user entered email address and validate it on the server side. Mind you we're not checking to see if it actually exists, as if we could, but rather if it is in the correct syntax of a valid email address.

Page One :
First off, lets just take a look at the code:

 function IsValidEmail(email)
  isitvalid = true
  dim names, name, i, c
  names = Split(email, "@")
  if UBound(names) <> 1 then
   isitvalid = false
   exit function
  end if
  for each name in names
   if Len(name) <=  0 then
    isitvalid = false
    exit function
   end if
   for i = 1 to Len(name)
    c = Lcase(Mid(name, i, 1))
    if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then
     isitvalid = false
     exit function
    end if
   next
   if Left(name, 1) = "." or Right(name, 1) = "." then
    isitvalid = false
    exit function
   end if
  next
  if InStr(names(1), ".") <= 0 then
   isitvalid = false 
   exit function
  end if
  i = Len(names(1)) - InStrRev(names(1), ".")
  if i <> 2 and i <> 3 then
   isitvalid = false
   exit function
  end if
  if InStr(email, "..") > 0 then
   is it valid=false
  end if
 end function

I will admit myself that this code is a little intimidating at first, but once you study it a bit, it is really very easily understood. While it is understood that you're at least a little understood with ASP and VBScripting, we're going to take a line by line look at this code.

Page Two :
First things first -- this is a function. If you don't know what a function is, it is simply a block of code that can be executed more than once within a single ASP file. The function syntax is as follows:

   function IsValidEmail(email)
    ...
   end function

Its that simple. "IsValidEmail" is the name of the function and "email" is the internal name of the string that you pass it. For the remainder of this tutorial, lets assume that we've passed the function the email address "charles@dimsdale.com", mine. Now let's get into the code within the function:

  isitvalid = true
  dim names, name, i, c
  names = Split(email, "@")

The first line is setting the default value of the variable isitvalid to be true -- pretty much saying that if its not declared invalid anywhere, its valid. The second line is just declaring some variables, while the third is splitting the internal variable "email" at the at sign (i.e. -- "@"). This split statement has created an array, names, with two elements, the first holding the string "charles" and the second holding the string "dimsdale.com".

  if UBound(names) <> 1 then
   isitvalid = false
   exit function
  end if

If you're not familiar with the UBound method, it simply returns returns the largest available subscript for the indicated dimension of an array. No dimension is indicated, so it assumes the first. In our case, UBound(names) returns a value of "1" because the "names" array holds two elements. (Remember! Arrays start with element zero.)

Page Three :

  for each name in names
   if Len(name) <=  0 then
    isitvalid = false
    exit function
   end if
   for i = 1 to Len(name)
    c = Lcase(Mid(name, i, 1))
    if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then
     isitvalid = false
     exit function
    end if
   next
   if Left(name, 1) = "." or Right(name, 1) = "." then
    isitvalid = false
    exit function
   end if
  next

"for each name in names" is giving a value to the name variable that we declared earlier. Basically what this line is doing is saying "for each element in the names array, do this". It simply assigns the value of said element in the array to the internal variable name. The code between this statement and the final next is executed for each element in the names array. On we go:

   if Len(name) <=  0 then
    isitvalid = false
    exit function
   end if

Basically, if the length of this element in the names array is less than or equal to zero, then it is an invalid email address. How could it be a valid email address if there is nothing on one side of the @?

   for i = 1 to Len(name)
    c = Lcase(Mid(name, i, 1))
    if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then
     isitvalid = false
     exit function
    end if
   next

Here we are running another variety of a for loop, but this it it is a "for to" loop. "Given that i = 1, do this until i equals the length of this element of the names array". Its really very simple. What we are doing here is going through, character by character, the value of name.

	c = Lcase(Mid(name, i, 1))

Variable "c" is given the value of Lcase(Mid(name, i, 1)). The MID statement is a very useful one, used for gathering substrings of larger strings. In our case, we are just gather one character and forcing it to be lower case by using the Lcase statement. The first time through the loop, variable c will hold the values of "c","h","a","r","l","e", and "s" while it will hold the values of "d", "i", "m", "s", "d", "a", "l", "e", ".", "c", "o", "m",

	if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0
          and not IsNumeric(c) then
		isitvalid = false
		exit function
	end	if

If the value of variable "c" is not a substring of string "abcdefghijklmnopqrstuvwxyz_-." (all valid characters in an email address), then the email address passed is invalid and the function is exited.

   if Left(name, 1) = "." or Right(name, 1) = "." then
    isitvalid = false
    exit function
   end if

If the leftmost character or the rightmost character in either string is a period, then the email address is not valid and the function is exited.

Page Four :

  if InStr(names(1), ".") <= 0 then
   isitvalid = false 
   exit function
  end if
  i = Len(names(1)) - InStrRev(names(1), ".")
  if i <> 2 and i <> 3 then
   isitvalid = false
   exit function
  end if
  if InStr(email, "..") > 0 then
   isitvalid=false
  end if

The string we passed the function,"charles@dimsdale.com", has been split into a two element array. Like I said before, the value of the first element is "charles" and the second "dimsdale.com". In order for this to be a valid email address, names(1), "dimsdale.com", must contain at least one period. Otherwise it would not be valid. Thus we have:

  if InStr(names(1), ".") <= 0 then
   isitvalid = false 
   exit function
  end if

And we proceed to:

  i = Len(names(1)) - InStrRev(names(1), ".")
  if i <> 2 and i <> 3 then
   isitvalid = false
   exit function
  end if

Introduction :
In this tutorial I intend to teach you how to take a user entered email address and validate it on the server side. Mind you we're not checking to see if it actually exists, as if we could, but rather if it is in the correct syntax of a valid email address.

Page One :
First off, lets just take a look at the code:

 function IsValidEmail(email)
  isitvalid = true
  dim names, name, i, c
  names = Split(email, "@")
  if UBound(names) <> 1 then
   isitvalid = false
   exit function
  end if
  for each name in names
   if Len(name) <=  0 then
    isitvalid = false
    exit function
   end if
   for i = 1 to Len(name)
    c = Lcase(Mid(name, i, 1))
    if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then
     isitvalid = false
     exit function
    end if
   next
   if Left(name, 1) = "." or Right(name, 1) = "." then
    isitvalid = false
    exit function
   end if
  next
  if InStr(names(1), ".") <= 0 then
   isitvalid = false 
   exit function
  end if
  i = Len(names(1)) - InStrRev(names(1), ".")
  if i <> 2 and i <> 3 then
   isitvalid = false
   exit function
  end if
  if InStr(email, "..") > 0 then
   is it valid=false
  end if
 end function

I will admit myself that this code is a little intimidating at first, but once you study it a bit, it is really very easily understood. While it is understood that you're at least a little understood with ASP and VBScripting, we're going to take a line by line look at this code.

Page Two :
First things first -- this is a function. If you don't know what a function is, it is simply a block of code that can be executed more than once within a single ASP file. The function syntax is as follows:

   function IsValidEmail(email)
    ...
   end function

Its that simple. "IsValidEmail" is the name of the function and "email" is the internal name of the string that you pass it. For the remainder of this tutorial, lets assume that we've passed the function the email address "charles@dimsdale.com", mine. Now let's get into the code within the function:

  isitvalid = true
  dim names, name, i, c
  names = Split(email, "@")

The first line is setting the default value of the variable isitvalid to be true -- pretty much saying that if its not declared invalid anywhere, its valid. The second line is just declaring some variables, while the third is splitting the internal variable "email" at the at sign (i.e. -- "@"). This split statement has created an array, names, with two elements, the first holding the string "charles" and the second holding the string "dimsdale.com".

  if UBound(names) <> 1 then
   isitvalid = false
   exit function
  end if

If you're not familiar with the UBound method, it simply returns returns the largest available subscript for the indicated dimension of an array. No dimension is indicated, so it assumes the first. In our case, UBound(names) returns a value of "1" because the "names" array holds two elements. (Remember! Arrays start with element zero.)

Page Three :

  for each name in names
   if Len(name) <=  0 then
    isitvalid = false
    exit function
   end if
   for i = 1 to Len(name)
    c = Lcase(Mid(name, i, 1))
    if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then
     isitvalid = false
     exit function
    end if
   next
   if Left(name, 1) = "." or Right(name, 1) = "." then
    isitvalid = false
    exit function
   end if
  next

"for each name in names" is giving a value to the name variable that we declared earlier. Basically what this line is doing is saying "for each element in the names array, do this". It simply assigns the value of said element in the array to the internal variable name. The code between this statement and the final next is executed for each element in the names array. On we go:

   if Len(name) <=  0 then
    isitvalid = false
    exit function
   end if

Basically, if the length of this element in the names array is less than or equal to zero, then it is an invalid email address. How could it be a valid email address if there is nothing on one side of the @?

   for i = 1 to Len(name)
    c = Lcase(Mid(name, i, 1))
    if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then
     isitvalid = false
     exit function
    end if
   next

Here we are running another variety of a for loop, but this it it is a "for to" loop. "Given that i = 1, do this until i equals the length of this element of the names array". Its really very simple. What we are doing here is going through, character by character, the value of name.

	c = Lcase(Mid(name, i, 1))

Variable "c" is given the value of Lcase(Mid(name, i, 1)). The MID statement is a very useful one, used for gathering substrings of larger strings. In our case, we are just gather one character and forcing it to be lower case by using the Lcase statement. The first time through the loop, variable c will hold the values of "c","h","a","r","l","e", and "s" while it will hold the values of "d", "i", "m", "s", "d", "a", "l", "e", ".", "c", "o", "m",

	if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0
          and not IsNumeric(c) then
		isitvalid = false
		exit function
	end	if

If the value of variable "c" is not a substring of string "abcdefghijklmnopqrstuvwxyz_-." (all valid characters in an email address), then the email address passed is invalid and the function is exited.

   if Left(name, 1) = "." or Right(name, 1) = "." then
    isitvalid = false
    exit function
   end if

If the leftmost character or the rightmost character in either string is a period, then the email address is not valid and the function is exited.

Page Four :

  if InStr(names(1), ".") <= 0 then
   isitvalid = false 
   exit function
  end if
  i = Len(names(1)) - InStrRev(names(1), ".")
  if i <> 2 and i <> 3 then
   isitvalid = false
   exit function
  end if
  if InStr(email, "..") > 0 then
   isitvalid=false
  end if

The string we passed the function,"charles@dimsdale.com", has been split into a two element array. Like I said before, the value of the first element is "charles" and the second "dimsdale.com". In order for this to be a valid email address, names(1), "dimsdale.com", must contain at least one period. Otherwise it would not be valid. Thus we have:

  if InStr(names(1), ".") <= 0 then
   isitvalid = false 
   exit function
  end if

And we proceed to:

  i = Len(names(1)) - InStrRev(names(1), ".")
  if i <> 2 and i <> 3 then
   isitvalid = false
   exit function
  end if

 ( 1 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions ( Threads: 5, Comments: 6 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. how to validate email using VBScript when we support UNICODE
  2. Different form of Email Validation
  3. Wrong email validation characters
  4. typo in original code
  5. E.mail validation ( 1 Reply ) This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.