ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 CoverYourASP --> Sending email from a form --> Part 2

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
336 active users
1711 visitors today
2551 pages today
how is this done?
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

CoverYourASP
Copyright © 1999-2008 James Shaw.
All rights reserved.

ASP.NET Blog
ASPRSS
ShawThing

Now open source with SourceForge!

The IsValidEmail( ) function

This function resides in the utils/email.asp Server Side Include. As you can see below, the function simply calls GetEmailRating( ), and displays a message if the function fails...

function IsValidEmail ( sEmail, nLevel )
{
   // test all email addresses sent in
   var sEmailList = sEmail.split ( /[\s;,]/ );
   var nEmail;

   for ( nEmail in sEmailList )
   {
      if ( hexVeLevelBad == GetEmailRating ( sEmailList [ nEmail ], nLevel ) )
      {
         Out ( '<center><b><font color="red">"' + sEmailList [ nEmail ] + '" is an invalid email address - try again!</font></b>' );
         Out ( '<br><a href="ValidateEmail.asp">(See how this email validation was done)</a></center><p>' );

         return false;
      }
   }

   return true;
}

...so we really have to look at GetEmailRating( ).

var hexVeLevelSyntax = 1;
var hexVeLevelDns = 2;
var hexVeLevelSmtp = 3;

function GetEmailRating ( sEmail, nLevel )
{
   // simple syntax validation if no Hexillion component
   if ( !bUseHexillion )
   {
      if ( IsValidEmailSyntax ( sEmail ) )
         return hexVeLevelSyntax;

      return hexVeLevelBad;
   }

   //...use HexValidEmail

To start with, I declare some global variables that are passed into these validation functions. Think of these as const's or enum's that are used to determine the level of confidence in the email address.

Pass in hexVeLevelDns and Hexillion's cool HexValidEmail component will ensure that the domain in the email address exists. These constants are also used as return values from the functions.

If we're not going to be taking advantage of HexValidEmail, and hence bUseHexillion is set to false in include/config.asp, we call a function that uses a regular expression to determine if the syntax of the email address is valid.

Thanks again to Ed Courtenay for donating the regular expression used to validate the syntax. I'll leave you to explore that and the rest of the source code concerning email validation by viewing the entire utils/email.asp source code at the end of the article.

For more information on HexValidEmail, and the 3 levels of validation it offers, see my live demonstration.

Part 3: Making up the email...

Featured sponsor
My favorite resources

Selling products online - read how James does it now



Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!