Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (14)


Validate email addressFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 3.x (or higher)
Category:
Internet / Web
Skill Level:
Scoring:
Last Update:
06/13/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet validation email address
Times Scored:
21
Visits:
13335
Uploader: Udo Nesshoever
Company:
Reference: N/A
 
Question/Problem/Abstract:
How to validate an email address
Answer:



This solution does better validation than article 1067 (http://www.delphi3000.com/article.asp?ID=1067) but is smaller (at least no component) than article 1346 (http://www.delphi3000.com/article.asp?ID=1346).

Updated on 2000-12-06 due to Phil's and Sven's comments.
Updated on 2002-06-13 due to Carlos' comment.
Thanks for improving!

function IsValidEmail(const Value: string): boolean;
  function CheckAllowed(const s: string): boolean;
  var
    i: integer;
  begin
    Result:= false;
    for i:= 1 to Length(s) do
    begin
      // illegal char in s -> no valid address
      if not (s[i] in ['a'..'z','A'..'Z','0'..'9','_','-','.']) then
        Exit;
    end;
    Result:= true;
  end;
var
  i: integer;
  namePart, serverPart: string;
begin // of IsValidEmail
  Result:= false;
  i:= Pos('@', Value);
  if (i = 0) or (pos('..', Value) > 0) then
    Exit;
  namePart:= Copy(Value, 1, i - 1);
  serverPart:= Copy(Value, i + 1, Length(Value));
  if (Length(namePart) = 0)         // @ or name missing
    or ((Length(serverPart) < 4))   // name or server missing or
    then Exit;                      // too short
  i:= Pos('.', serverPart);
  // must have dot and at least 3 places from end
  if (i < 2) or (i > (Length(serverPart) - 2)) then
    Exit;
  Result:= CheckAllowed(namePart) and CheckAllowed(serverPart);
end;






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
more characters are valid in names than servers
    Heywood Jablowme (Jan 6 2008 11:39PM)

servernames are limited to valid domains, but usernames can contain a much wider variety of characters than you allow. You're also restricting things to narrow strings only but that's understandable and arguably correct (RFC2822 says 7 bit only).

So I added a boolean to CheckAllowed and allow extra characters in the name part as per http://en.wikipedia.org/wiki/E-mail_address
Respond

RE: more characters are valid in names than servers
Udo Nesshoever (Jan 7 2008 9:03AM)

Could you provide me with your latest changes so I can update the code here?
Respond

A better version
    ERIC CHAN (Mar 20 2001 3:33AM)

A better version:

FUNCTION IsValidEmail(CONST Value: String): boolean;
  FUNCTION CheckAllowed(CONST s: String): boolean;
  VAR i: Integer;
  BEGIN
  Result:= False;
  FOR i:= 1 TO Length(s) DO // illegal char in s -> no valid address
  IF NOT (s[i] IN ['a'..'z','A'..'Z','0'..'9','_','-','.']) THEN Exit;
  Result:= true;
  END;
VAR
  i,len: Integer;
  namePart, serverPart: String;
BEGIN // of IsValidEmail
  Result:= False;
  i:= Pos('@', Value);
  IF (i=0) OR (Pos('..',Value) > 0) THEN Exit;
  namePart:= Copy(Value, 1, i - 1);
  serverPart:= Copy(Value,i+1,Length(Value));
  len:=Length(serverPart);
  // must have dot and at least 3 places from end, 2 places from begin
  IF (len<4) OR
     (Pos('.',serverPart)=0) OR
     (serverPart[1]='.') OR
     (serverPart[len]='.') OR
     (serverPart[len-1]='.') THEN Exit;
  Result:= CheckAllowed(namePart) AND CheckAllowed(serverPart);
END;

Respond

RE: A better version
Udo Nesshoever (Jun 7 2001 10:51AM)

This version accepts the email addres '@domain.com' (no character before the @).And both versions, yours and mine have difficulties with the new TLDs like .info.

Cheers,
Udo
Respond

inaccure...
    Ralf Steinhaeusser (Feb 13 2001 4:24AM)

a@a.b. still looks like a good email to the routine, as well as a@.com
Respond

RE: inaccure...
Carlos Junior (May 29 2002 8:06PM)

To turn False the validation of the wrong address mail@.com
and turn True the validation of the correct address mail@mail.br

replace
if (i = 0) or (i >= (Length(serverPart) - 2)) then Exit;

with
if (i < 2) or (i > (Length(serverPart) - 2)) then Exit;

i < 2: must have dot, but not in the 1st place of serverPart;
i > (Length(serverPart) - 2): the TLD must have 2 chars (.br, .it ...).
Respond

RE: RE: inaccure...
Udo Nesshoever (Jun 13 2002 12:06PM)

Thanks. I added your improvment.
Respond

Validate email address
    Scott Mattes (Dec 11 2000 8:16AM)

What about validating that the validated host is in fact real? I like to put in asdf@asdf.asd when asked for an email address at sites where I don't really want to leave an address.
Respond

RE: Validate email address
Udo Nesshoever (Dec 11 2000 9:01AM)

this is designed to be a syntactic validation rather than a semantic one. Even more the validation of a real server is a complete different subject. But that's wandering around my mind, too. So feel free to extend my procedure ;)
Maybe after D6 came out w/built in Indy I'll write a procedure based on that suite of Inet components.
Respond

Small Addition
    Sven Opitz (Dec 5 2000 9:35AM)

Hi Udo,

perhaps you should add the following to make the test a little bit better:

  if Pos( '..', Value ) > 0 then
  begin
    Exit;
  end;

otherwise very nice, thanx

Bye
Sven
Respond

article 1649
    Phil (Nov 28 2000 1:52AM)

Thanks. Very useful - i was just looking for this. But :-
the 'server part' should be a minimum of 4 not 5 characters. e.g.
a.tv, a.it, etc
{unless its just for US addresses :-( }

Respond

RE: article 1649
Paul Harker (Dec 11 2000 3:41AM)

On the same note, the dot "." need only be 2 spaces from the end, for ".tv" and ".cc" or ".ca" extensions.
Respond

RE: RE: article 1649
Udo Nesshoever (Dec 11 2000 9:03AM)

You're correct and I've corrected that ;)
Respond

Great !!
    PEPS ^^ (Nov 27 2000 12:39PM)

Dear Udo,

thanks for the comments at the beginning of this article - why you posted it and the difference to the other articles here discussing this topic !

that makes it A LOT easier to maintaine this site and keep the quality !

regards
bernhard a.

DELPHI3000 - Editorial

Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
S. Carter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)