Server Side Email Addresses Validation using ASP
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
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.










Comments
No comments yet.
Leave a comment