开发者

Delphi: validate email without regular expressions

开发者 https://www.devze.com 2023-01-13 15:45 出处:网络
Since Delphi does not have any regular expressions library built-in, have y开发者_JAVA技巧ou seen a good function to validate email addresses at least to some degree in using only Delphi RTL/VCL?

Since Delphi does not have any regular expressions library built-in, have y开发者_JAVA技巧ou seen a good function to validate email addresses at least to some degree in using only Delphi RTL/VCL?

I don't want to link additional *.dll to my product integrating regular expression support and I need also Delphi 7 compatibility. Embedding regex library into exe increases it's size and I doubt it worths adding next 100k because you just need 10-50-lines function of email validation.


As requested I'm making my comment an answer..

http://www.howtodothings.com/computers/a1169-validating-email-addresses-in-delphi.html

Thanks!


The big problem on email address validation is that RFC 822 is so broad, that no regular expression will help.

The article I Knew How To Validate An Email Address Until I Read The RFC describes this well.
It quotes the RFC the local-part MUST be interpreted and assigned semantics only by the host specified in the domain part of the address (the local part is the part before the @ sign in an email address).

So:
the only way to check if an email address is valid, is to setup an SMTP connection to the host accepting mail for that particular domain, and start up the email handshaking process with the email address you are trying to verify.

This is what a lot of anti-SPAM software does to verify sender email addresses: they contact the SMTP server of the sender of the email, try to setup an SMTP handshake, and if that goes OK, they rate the incoming email as more like to not be SPAM.

You can use the Indy SMTP client component to send mail; the accepted answer to this question explains how.

--jeroen


For the old version of Delphi that doesn't have Regular expression :

function TForm1.IsValidEmail(email: string): boolean;
const
  charslist = ['_', '-', '.', '0'..'9', 'A'..'Z', 'a'..'z'];
var
  Arobasc, lastpoint : boolean;
  i, n : integer;
  c : char;
begin
  n := Length(email);
  i := 1;
  Arobasc := false;
  lastpoint := false;
  result := true;
  while (i <= n) do begin
    c := email[i];
    if c = '@' then
    begin
      if Arobasc then  // Only 1 Arobasc
      begin
        result := false;
        exit;
      end;
      Arobasc := true;
    end
    else if (c = '.') and Arobasc then  // at least 1 . after arobasc
    begin
      lastpoint := true;
    end
    else if not(c in charslist) then  // valid chars
    begin
      result := false;
      exit;
    end;
    inc(i);
  end;
  if not(lastpoint) or (email[n] = '.')then  // not finish by . and have a . after arobasc
    result := false;
end;

For the one who like to use RegExpression using Database server

function TForm1.IsValidEmail(const AMail: string): Boolean;
var
  lQry: TSQLQuery;
begin
  // no REGEXP in Delphi 2009 -> use of REGEPX Oracle
  lQry := TSQLQuery.Create();
  lQry.SQLConnection := SQLConnection1;
  try
    vQry.SQL.Text := 'SELECT REGEXP_SUBSTR( ' + QuotedStr(aMail) + ', ''^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{1,}$'' ) "EMAIL" FROM DUAL';  // ORACLE
    // vQry.SQL.Text := 'SELECT ' + QuotedStr(aMail) + ' REGEXP ''^[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9.-]@[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]\.[a-zA-Z]{2,4}$'';  // MySQL
    lQry.Open;
    Result := not lQry.Eof and ( lQry.FieldByName( 'EMAIL' ).AsString = AMail );
  finally
    FreeAndNil(lQry);
  end;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消