|
Technical Interview Questions
Visual Basic Interview Question
.NET Web Interview Questions
.NET
Interview Questions
C#
Interview Questions
.........More
Source Codes
Asp .NET Source Codes
Asp VB Script Source Codes
.........More
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
Asp/VB Script Source Codes
Server Side Email Addresses
Validation
<SPAN class=heading>Introduction :<BR></SPAN><BR>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.
<P><SPAN class=smallHeading>Page One :<BR></SPAN><BR>First off, lets just take a
look at the code:</P><PRE> 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</PRE>
<P>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</P>
<P><SPAN class=smallHeading>Page Two :<BR></SPAN><BR>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:</P><PRE> function IsValidEmail(email)
...
end function</PRE>
<P>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:</P><PRE> isitvalid = true
dim names, name, i, c
names = Split(email, "@")</PRE>
<P>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".</P><PRE> if UBound(names)
<> 1 then
isitvalid = false
exit function
end if</PRE>
<P>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.)</P>
<P><SPAN class=smallHeading>Page Three :</SPAN></P><PRE> 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</PRE>
<P>"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:</P><PRE>
if Len(name) <= 0 then
isitvalid = false
exit function
end if</PRE>
<P>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 @?</P><PRE> 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</PRE>
<P>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.</P><PRE> c = Lcase(Mid(name,
i, 1))</PRE>
<P>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",</P><PRE> if InStr("abcdefghijklmnopqrstuvwxyz_-.",
c) <= 0
and not IsNumeric(c) then
isitvalid = false
exit function
end if</PRE>
<P>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.</P><PRE> if Left(name, 1) = "." or
Right(name, 1) = "." then
isitvalid = false
exit function
end if</PRE>
<P>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.</P>
<P><SPAN class=smallHeading>Page Four :</SPAN></P><PRE> 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</PRE>
<P>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:</P><PRE> if InStr(names(1), ".") <= 0 then
isitvalid = false
exit function
end if</PRE>
<P>And we proceed to: <PRE> i = Len(names(1)) - InStrRev(names(1), ".")
if i <> 2 and i <> 3 then
isitvalid = false
exit function
end if</PRE><PRE><P>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.</P><PRE> if
InStr(email, "..") > 0 then
isitvalid=false
end if</PRE><PRE> if InStr(email, "..") > 0 then
isitvalid=false
end if</PRE><P>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.</P><P><SPAN class=heading><BR>Summary :</SPAN></P><P><BR>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:</P><PRE> IsValidEmail("charles@dimsdale.com")</PRE><P>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.</P></PRE>
<<<----- Return to
Asp/VB Script Source
Code Questions Page.
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
Microsoft .Net Interview Questions
for more Microsoft
.Net Interview Questions with Answers.
Check
.Net Database Interview
Questions for more .Net Database Interview Questions with answers
Check
Job Interview Questions
for more Interview Questions with Answers
|