?
Solved

Verify e-mail address is valid

Posted on 2001-08-03
7
Medium Priority
?
213 Views
Last Modified: 2010-04-06
How do I verify an e-mail address typed by a user into an edit box is valid ?. i.e. of the correct format name@xxxxx.xxx

Thanks
Paul.
0
Comment
Question by:pr_wainwright
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

by:VSF
ID: 6350136
I will try to come up with a e-mail verification funtion and will let you know!VSF
www.victory.hpg.com.br

This is not exactly what you need but I think it can help

function VerifyURL(S: string): Boolean;
const
BADCHARS = ';*<>{}[]|\()^!';
var
p, x, c, count, i: Integer;
begin
Result := False;
if (Length(S) > 5) and (S[Length(S)] <> '.') and (Pos(S, '..') = 0) then
   begin
   for i := Length(BADCHARS) downto 1 do
       begin
       if Pos(BADCHARS[i], S) > 0 then
          begin
          exit;
          end;
       end;
   for i := 1 to Length(S) do
       begin
       if (Ord(S[i]) < 33) or (Ord(S[i]) > 126) then
          begin
          exit;
          end;
       end;
   if ((Pos('www.',LowerCase(S)) = 1) and (Pos('.', Copy(S, 5, Length(s))) > 0) and (Length(S) > 7)) or ((Pos('news:', LowerCase(S)) = 1) and (Length(S) > 7) and (Pos('.', Copy(S, 5, Length(S))) > 0)) then
       begin
       end
   else if ((Pos('mailto:', LowerCase(S)) = 1) and (Length(S) > 12) and (Pos('@', S) > 8) and (Pos('.', S) > 10) and (Pos('.', S) > (Pos('@', S) +1))) or ((Length(S) > 6) and (Pos('@', S) > 1) and (Pos('.', S) > 4) and (Pos('.', S) > (Pos('@', S) +1))) then
            begin
            Result := True;
            Exit;
            end
   else if ((Pos('http://', LowerCase(S)) = 1) and (Length(S) > 10) and (Pos('.', S) > 8)) or ((Pos('ftp://', LowerCase(S)) = 1) and (Length(S) > 9) and (Pos('.', S) > 7)) then
           begin
           Result := True;
           Exit;
           end
        else
           begin
           Result := True;
           end;
   for Count := 1 to 4 do
       begin
       p := Pos('.',S) - 1;
       if p < 0 then
          begin
          p := Length(S);
          end;
        Val(Copy(S, 1, p), x, c);
        if ((c <> 0) or (x < 0) or (x > 255) or (p>3)) then
           begin
           Result := False;
           Break;
           end;
        Delete(S, 1, p + 1);
        end;
   if (S <> '') then
       begin
       Result := False;
       end;
   end;
end;
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 6350149
A quick and dirty one I use is:

if (Pos('@', Edit1.Text) = 0) or (Pos('.', Edit1.Text) = 0) then ShowMessage('Invalid E-Mail Address');
0
 
LVL 3

Expert Comment

by:VSF
ID: 6350169
I will try to come up with a e-mail verification funtion and will let you know!VSF
www.victory.hpg.com.br

This is not exactly what you need but I think it can help

function VerifyURL(S: string): Boolean;
const
BADCHARS = ';*<>{}[]|\()^!';
var
p, x, c, count, i: Integer;
begin
Result := False;
if (Length(S) > 5) and (S[Length(S)] <> '.') and (Pos(S, '..') = 0) then
   begin
   for i := Length(BADCHARS) downto 1 do
       begin
       if Pos(BADCHARS[i], S) > 0 then
          begin
          exit;
          end;
       end;
   for i := 1 to Length(S) do
       begin
       if (Ord(S[i]) < 33) or (Ord(S[i]) > 126) then
          begin
          exit;
          end;
       end;
   if ((Pos('www.',LowerCase(S)) = 1) and (Pos('.', Copy(S, 5, Length(s))) > 0) and (Length(S) > 7)) or ((Pos('news:', LowerCase(S)) = 1) and (Length(S) > 7) and (Pos('.', Copy(S, 5, Length(S))) > 0)) then
       begin
       end
   else if ((Pos('mailto:', LowerCase(S)) = 1) and (Length(S) > 12) and (Pos('@', S) > 8) and (Pos('.', S) > 10) and (Pos('.', S) > (Pos('@', S) +1))) or ((Length(S) > 6) and (Pos('@', S) > 1) and (Pos('.', S) > 4) and (Pos('.', S) > (Pos('@', S) +1))) then
            begin
            Result := True;
            Exit;
            end
   else if ((Pos('http://', LowerCase(S)) = 1) and (Length(S) > 10) and (Pos('.', S) > 8)) or ((Pos('ftp://', LowerCase(S)) = 1) and (Length(S) > 9) and (Pos('.', S) > 7)) then
           begin
           Result := True;
           Exit;
           end
        else
           begin
           Result := True;
           end;
   for Count := 1 to 4 do
       begin
       p := Pos('.',S) - 1;
       if p < 0 then
          begin
          p := Length(S);
          end;
        Val(Copy(S, 1, p), x, c);
        if ((c <> 0) or (x < 0) or (x > 255) or (p>3)) then
           begin
           Result := False;
           Break;
           end;
        Delete(S, 1, p + 1);
        end;
   if (S <> '') then
       begin
       Result := False;
       end;
   end;
end;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:pr_wainwright
ID: 6350814
VSF,
    Your function works fine for most email addresses but my company's email format is 'paul.wainwright@unilever.com' which is invalid. The first '.' makes the email invalid. How would i modify the code to accept this format ?. I have tried but can't get it to work properly !.

Thanks
Paul.
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6351568
You could try something like this:

function IsEmailAddressInvalid(const S : string) : Boolean;   // Returns True if Email Addy is Invalid
var
  I : Integer;
  Temp : string;
begin
  Result := (Trim(S) = '') or
            (Pos(' ', AnsiLowerCase(S)) > 0) or
            (Pos('a', AnsiLowerCase(S)) > 0) or
            (Pos('o', AnsiLowerCase(s)) > 0) or
            (Pos('u', AnsiLowerCase(S)) > 0) or
            (Pos('?', AnsiLowerCase(S)) > 0) or
            (Pos('[', AnsiLowerCase(S)) > 0) or
            (Pos(']', AnsiLowerCase(S)) > 0) or
            (Pos('(', AnsiLowerCase(S)) > 0) or
            (Pos(')', AnsiLowerCase(S)) > 0) or
            (Pos(':', AnsiLowerCase(S)) > 0);
  if Result then
    Exit;
  I := Pos('@', S);
  Result := (I = 0) or (I = 1) or (I = Length(S));
  if Result then
    Exit;
  Result := (Pos('@', Copy(S, I + 1, Length(S) - 1)) > 0);
  if Result then
    Exit;
  Temp := Copy(S, I + 1, Length(S));
  Result := Length(Temp) <= 1;
  if Result then
    Exit;
  I := Pos('.', Temp);
  Result := (I = 0) or (I = 1) or (I = Length(Temp));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsEmailAddressInvalid('paul.wainwright@unilever.com') then
    ShowMessage('Email Address is Invalid.')
  else
    ShowMessage('Email Address is valid.');
end;
0
 
LVL 5

Accepted Solution

by:
alanwhincup earned 200 total points
ID: 6351577
The above example was slightly wrong (Some chars didnt post correctly):

function IsEmailAddressInvalid(const S : string) : Boolean;   // Returns True if Email Addy is Invalid
var
  I : Integer;
  Temp : string;
begin
  Result := (Trim(S) = '') or
            (Pos(' ', AnsiLowerCase(S)) > 0) or
            (Pos(Chr(228), AnsiLowerCase(S)) > 0) or
            (Pos(Chr(246), AnsiLowerCase(s)) > 0) or
            (Pos(Chr(252), AnsiLowerCase(S)) > 0) or
            (Pos(Chr(223), AnsiLowerCase(S)) > 0) or
            (Pos('[', AnsiLowerCase(S)) > 0) or
            (Pos(']', AnsiLowerCase(S)) > 0) or
            (Pos('(', AnsiLowerCase(S)) > 0) or
            (Pos(')', AnsiLowerCase(S)) > 0) or
            (Pos(';', AnsiLowerCase(S)) > 0) or
            (Pos('*', AnsiLowerCase(S)) > 0) or
            (Pos('<', AnsiLowerCase(S)) > 0) or
            (Pos('>', AnsiLowerCase(S)) > 0) or
            (Pos('{', AnsiLowerCase(S)) > 0) or
            (Pos('}', AnsiLowerCase(S)) > 0) or
            (Pos('\', AnsiLowerCase(S)) > 0) or
            (Pos('^', AnsiLowerCase(S)) > 0) or
            (Pos('!', AnsiLowerCase(S)) > 0) or
            (Pos(':', AnsiLowerCase(S)) > 0);
  if Result then
    Exit;
  I := Pos('@', S);
  Result := (I = 0) or (I = 1) or (I = Length(S));
  if Result then
    Exit;
  Result := (Pos('@', Copy(S, I + 1, Length(S) - 1)) > 0);
  if Result then
    Exit;
  Temp := Copy(S, I + 1, Length(S));
  Result := Length(Temp) <= 1;
  if Result then
    Exit;
  I := Pos('.', Temp);
  Result := (I = 0) or (I = 1) or (I = Length(Temp));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsEmailAddressInvalid('paul.wainwright@unilever.com') then
    ShowMessage('Email Address is Invalid.')
  else
    ShowMessage('Email Address is valid.');
end;
0
 
LVL 1

Author Comment

by:pr_wainwright
ID: 6351878
Alan,
     Your function is exactly what i require.

Thanks
Paul.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question