Solved

Verify E-Mail Address Format

Posted on 2000-03-02
11
474 Views
Last Modified: 2010-09-23
I have seen PERL script code that verifies the correctness of an e-mail address format.  Not specifically that the address is a fully valid address (someone on the other end) but simply that it has the exact correct format for an e-mail address.

I was wondering if anyone has DELPHI code to do such a thing?

Jim
0
Comment
Question by:JimboKern69
[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
  • +5
11 Comments
 

Expert Comment

by:WildMan
ID: 2577898
Try

Verified := Pos('@', EmailAddress)<>0);

Hope this help.

0
 
LVL 6

Expert Comment

by:edey
ID: 2577901
var
 email : string;
 dummy : integer;
 valid : boolean;
....
 dummy := pos('@',email);
 if (pos('.',email)>dummy+1)and(dummy > 1)then
   valid := true
 else
     valid := false;



GL
Mike
0
 
LVL 3

Expert Comment

by:Alisher_N
ID: 2577936
edey, what about john.smith@mycompany.com ?

there is also another (rare) notation like alisher!bcc.com.uz!alisher
there are also local users at server, for example my email alisher@bcc.com.uz and my friend has friend@bcc.com.uz
in this case I can send him an Email using just 'friend'
0
Industry Leaders: 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!

 

Author Comment

by:JimboKern69
ID: 2577970
Let me try to stear this in the right direction...

I want something that does more than simply search for an '@' symbol in the string.  What if there are 2 '@'s?  Other conditions...anyway...

Here is the PERL script code that I would like "translated" if possible.

     
     --------------------------------------------------
     if($FORM{'email'}) {
       $FORM{'email'} =~ s/ //g;
       if($FORM{'email'} =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
     $FORM{'email'} !~
     /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/)
     {&InvalidEmailAddress};
     }
     --------------------------------------------------
     
     Note: (In this case, "$FORM{'email'}" is the email address that the
     user inputs, and "InvalidEmailAddress" is the subroutine that you
     would jump to if the email address is invalid).
0
 
LVL 6

Expert Comment

by:edey
ID: 2578005
1)alisher - yes you're def. right.  there are many more formats then validated by my code, though it gave an example of how to go about checking a string for certain content.

2)jimbo - the above perl code clearly demonstrates why it (perl) is a &remarkably* powerfull language for string manipulation, essentialy what it does is check a string against, let's see, about 200 wild carded sequences using (of course) wild cards, sets, alpha/numerical values & punctuation.  This would require a *massive* case statement in delphi.


GL
Mike
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2578886
I'd try a different tack. In this function I check a few common email address characteristics and increment the possibly of a valid address accordingly. this way you can decide that if the function returns at least a value of x, you have a probable email address.


Function TForm1.IsValidEmail(addr:string):integer;
var teststring:string;
    ProbableGood:integer;
begin
   Teststring:=addr;
   ProbableGood:=0;
   if Pos('@',testString)>0) then inc(ProbableGood);
   IF (TestString[length(testString)-3]='.') then inc (ProbableGood);

result:=ProbableGood;
end;




Good luck!!
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2578920
Enpanded this a little to look at domains as well...




var teststring:string;
    ProbableGood:integer;
    Domain:string;
begin
   Teststring:=addr;
   ProbableGood:=0;
   if Pos('@',testString)>0 then inc(ProbableGood);
   IF (TestString[length(testString)-3]='.') then inc (ProbableGood);
   Domain:=Copy(Teststring,Pos('.',TestString),4);
   if Uppercase(domain)='.COM' then inc(ProbableGood);
   if Uppercase(domain)='.EDU' then inc(ProbableGood);
   if Uppercase(domain)='.NET' then inc(ProbableGood);
   if Uppercase(domain)='.ORG' then inc(ProbableGood);
   if Uppercase(domain)='.GOV' then inc(ProbableGood);
result:=ProbableGood;
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2579323
Hi all,

what about using the same power in Delphi as Perl offers :-)? I'm just finishing my (freeware) Unicode unit which contains a regular expression search engine. I know of at least two other libraries which offer regular expression search as well (search for RegEx on the usual Delphi servers). So, Jimbo, use REs to compare a given eMail address with the form you gave above.

Ciao, Mike
0
 

Accepted Solution

by:
JimLane earned 100 total points
ID: 2598001
Jimbo,

According to your Perl code, here's what the "rules" are for deciding whether or not an email address is in a valid format:

AN EMAIL ADDRESS MUST NOT HAVE:
1) Two @ characters in a row (@@)
2) Two periods in a row (..)
3) An @ followed by a period (@.)
4) A period followed by an @ (.@)
5) It cannot end in a period (.)

AN EMAIL ADDRESS MUST HAVE:
1) Before the @ character, it can have ANYTHING (except for any of the above).
2) After the @ character, and up to the FINAL period (.), it can have any of the following:
A-Z, a-z, 0-9, a dash "-", or a period "."
3) After the final period (.) and until the end of the address, it can be 2 or 3 characters long if ALL those characters are in the range A-Z or a-z, or 1, 2 or 3 characters long if they are ALL numerics (0-9).

Here's some code that shows how to do exactly what your Perl code is doing:

var Address: String;
begin
  Address := 'test@test.com';

  //This will remove any spaces
  while Pos(' ', Address) > 0 do
  Delete(Address, Pos(' ', Address), 1);

  If VerifyEmail(Address) = TRUE Then
  ShowMessage('Valid Email Address')
  else
  ShowMessage('Invalid Email Address');
end;


function VerifyEmail(Email: String):Boolean;
var a, b: String;
begin
  VerifyEmail := TRUE;
  a := '';
  b := '';

  If Pos('@', Email) = 0 Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;

  If Pos('@', Copy(Email, Pos('@', Email) + 1, Length(Email))) <> 0 Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;

  If Pos('.', Email) = 0 Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;

  If Pos('..', Email) <> 0 Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;

  If Pos('@.', Email) <> 0 Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;

  If Pos('.@', Email) <> 0 Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;

  If Copy(Email, Length(Email), 1) = '.' Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;

  a := Copy(Email, Pos('@', Email) + 1, Length(Email));

  If Pos('.', a) = 0 Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;

  Repeat
  b := Copy(a, Length(a), 1) + b;
  a := Copy(a, 1, Length(a) - 1);
  Until Copy(a, Length(a), 1) = '.';
  a := Copy(a, 1, Length(a) - 1);

  Repeat
  If ((a[1] < 'A') or (a[1] > 'z') or ((a[1] >= '[') and (a[1] <= '`'))) and not ((a[1] >= '0') and (a[1] <= '9')) and not (a[1] = '-') and not (a[1] = '.') Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;
  a := Copy(a, 2, Length(a));
  Until a = '';

  If (Length(b) < 1) or (Length(b) > 3) Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;

  If Length(b) = 1 Then
  begin
    If (b < '0') or (b > '9') Then
    begin
      VerifyEmail := FALSE;
      exit;
    end;
  end;

  If Length(b) = 2 Then
  begin
    If (b[1] >= '0') and (b[1] <= '9') Then
    begin
      If (b[2] < '0') or (b[2] > '9') Then
      begin
        VerifyEmail := FALSE;
        exit;
      end;
    end
    else
    begin
      If (b[2] >= '0') and (b[2] <= '9') Then
      begin
        VerifyEmail := FALSE;
        exit;
      end;
    end;
  end;

  If Length(b) = 3 Then
  begin
    If (b[1] >= '0') and (b[1] <= '9') Then
    begin
      If (b[2] < '0') or (b[2] > '9')or (b[3] < '0') or (b[3] > '9') Then
      begin
        VerifyEmail := FALSE;
        exit;
      end;
    end
    else
    begin
      If ((b[2] >= '0') and (b[2] <= '9')) or ((b[3] >= '0') and (b[3] <= '9')) Then
      begin
        VerifyEmail := FALSE;
        exit;
      end;
    end;
  end;

  Repeat
  If ((b[1] < 'A') or (b[1] > 'z') or ((b[1] >= '[') and (b[1] <= '`'))) and not ((b[1] >= '0') and (b[1] <= '9')) Then
  begin
    VerifyEmail := FALSE;
    exit;
  end;
  b := Copy(b, 2, Length(b));
  Until b = '';
end;
0
 

Author Comment

by:JimboKern69
ID: 2598101
JimLane,

That was AWESOME!  Copy - Paste - Compile - WORKS!

Thanks so much!

Jimbo
0
 

Expert Comment

by:Delphiwizard
ID: 33749279
It doesn't check how many characters are in front of the @
Does this mean an emailaddress can start with a @?
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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