How to check for a valid EMail Address format in Delphi

Now a days most of applications have facility to send Email to Users with some information. So before send Email we generally check for valid Email address format. Here I have 2 approaches how to validate an Email address format in Delphi.

1. Validating Email addresses format using Regular Expression
A regular expression is a special text that we can use as a search pattern. So for this we can use a regular expression string to check Email address syntax. And Delphi XE or later versions provide TRegEx class to use regular expression functions. So we can use this approach only on Delphi XE or latter versions. But for this we have to use System.RegularExpressions unit in Uses part. 
....

    function IsMatch(const Input, Pattern: string): boolean;
    function IsValidEmailRegEx(const EmailAddress: string): boolean;
....
function TForm1.IsMatch(const Input, Pattern: string): boolean;
begin
  Result := TRegEx.IsMatch(Input, Pattern);
end;
function TForm1.IsValidEmailRegEx(const EmailAddress: string): boolean;
const
  EMAIL_REGEX = '^((?>[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])'
             +'[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)'
             +'(?>\.?[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])'
             +'[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]'
             +'{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d))'
             +'{4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\'
             +'[\x01-\x7f])+)\])(?(angle)>)$';
begin
  Result := IsMatch(EmailAddress, EMAIL_REGEX);
end;
....
Use of above functions....
procedure TForm1.btnValidateEmailregxClick(Sender: TObject);
begin
  if IsValidEmailRegEx(EditEmail.Text) then
    ShowMessage('Valid Email Address')
  else
    ShowMessage('InValid Email Address');
end;

2. To check Email address format manually with a user defined function in Delphi
In this part I have created a function in Delphi to check all characters of the Email address that the character is a valid character for Email or not.
.....
    function IsValidEmail(const Value: string): boolean;
.....
function TForm1.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 - 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
  Result:= False;

  i:= Pos('@', Value);
  if (i = 0) then
    Exit;

  if(pos('..', Value) > 0) or (pos('@@', Value) > 0) or (pos('.@', Value) > 0)then
    Exit;

  if(pos('.', Value) = 1) or (pos('@', Value) = 1) then
    Exit;

  namePart:= Copy(Value, 1, i - 1);
  serverPart:= Copy(Value, i + 1, Length(Value));
  if (Length(namePart) = 0)  or (Length(serverPart) < 5)    then
    Exit;                      // too short

  i:= Pos('.', serverPart);
  // must have dot and at least 3 places from end
  if (i=0) or (i>(Length(serverPart)-2)) then
    Exit;

  Result:= CheckAllowed(namePart) and CheckAllowed(serverPart);
end;
.....
procedure TForm1.btnValidateEmailClick(Sender: TObject);
begin
  if IsValidEmail(EditEmail.Text) then
    ShowMessage('Valid Email Address')
  else
    ShowMessage('InValid Email Address');
end;

Please note that 1st approach will not work on Delphi versions before Delphi XE because handling of regular expression functions are introduced from Delphi XE. So for Delphi 2010, 2006, 7, 6 or lower versions use 2nd approach to validate a Email address.

Popular posts from this blog

How to get WINDOWS special directories path in Delphi?

How to send Email in Delphi?

MS Excel Automation in Delphi