Solved

Verify E-Mail Address Format

Posted on 2000-03-02
11
471 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi procedure  - Unsatisfied forward or external declaration. 2 184
Making delphi communicate with a c# service 16 111
Delphi 2 69
CheckListBox usage 3 71
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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