|
|
Server Side Email Addresses Validation using VBScript - ASP Webmaster Tips, Knowledge Base Webmaster Tools
| Home > ASP > Server Side Email Addresses Validation using VBScript | |
| | Category | : ASP | | Written by | : Admin | | Date | : 2008-11-10 | | Rating | : 0 | Voted : 0 times | | Hit | : 458 | | | | |
| 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
We are declaring i to equal the length of the second element of the names array minus the numeric position of the first occurrence of a period, starting from the right end of the string. If variable i does not equal "2" or "3", then the email address is invalid. What this little snippet does here is checking for is to see how long the user specified their TLD (Top-Level Domain [i.e. -- .com,.net,.org,etc.]) to be. And since top level domains do not exist that have more than three characters, the period cannot be more than three characters from the left and the entered email address be valid. If it is determined to be an invalid email address, the function is exited.
if InStr(email, "..") > 0 then
isitvalid=false
end if
if InStr(email, "..") > 0 then
isitvalid=false
end if
If anywhere in the passed string is the substring "..", the email address is invalid because a valid email address cannot have two periods right together. No "exit function" statement is necessary here because the function is finally over.
Summary :
I hope that I've taught you a few things over the course of this tutorial. Additionally, you may be wondering how to execute the function. Its really very simple: just use the following line of code:
IsValidEmail("charles@dimsdale.com")
Of course, you can replace "charles@dimsdale.com" with your own email address, or more effectively the name of a variable that holds a user entered email address.
If you have any questions, feel free to write me at charles@dimsdale.com or post a question in the FORUM. I'd be glad to help.
|
|