首页  编辑  

判断字符串是否是合法的邮件地址

Tags: /超级猛料/String.字符串处理/   Date Created:

判断字符串是否是合法的邮件地址

邮件地址格式符合RFC 822

 function ValidEmail(email: string): boolean;

 // Returns True if the email address is valid

 // Author: Ernesto D'Spirito  

 const

   // Valid characters in an "atom"

   atom_chars = [#33..#255] - ['(', ')', '', '@', ',', ';', ':',

                               '\', '/', '"', '.', '[', ']', #127];

   // Valid characters in a "quoted-string"

   quoted_string_chars = [#0..#255] - ['"', #13, '\'];

   // Valid characters in a subdomain

   letters = ['A'..'Z', 'a'..'z'];

   letters_digits = ['0'..'9', 'A'..'Z', 'a'..'z'];

   subdomain_chars = ['-', '0'..'9', 'A'..'Z', 'a'..'z'];

 type

   States = (STATE_BEGIN, STATE_ATOM, STATE_QTEXT, STATE_QCHAR,

     STATE_QUOTE, STATE_LOCAL_PERIOD, STATE_EXPECTING_SUBDOMAIN,

     STATE_SUBDOMAIN, STATE_HYPHEN);

 var

   State: States;

   i, n, subdomains: integer;

   c: char;

 begin

   State := STATE_BEGIN;

   n := Length(email);

   i := 1;

   subdomains := 1;

   while (i       c := email[i];

     case State of

     STATE_BEGIN:

       if c in atom_chars then

         State := STATE_ATOM

       else if c = '"' then

         State := STATE_QTEXT

       else

         break;

     STATE_ATOM:

       if c = '@' then

         State := STATE_EXPECTING_SUBDOMAIN

       else if c = '.' then

         State := STATE_LOCAL_PERIOD

       else if not (c in atom_chars) then

         break;

     STATE_QTEXT:

       if c = '\' then

         State := STATE_QCHAR

       else if c = '"' then

         State := STATE_QUOTE

       else if not (c in quoted_string_chars) then

         break;

     STATE_QCHAR:

       State := STATE_QTEXT;

     STATE_QUOTE:

       if c = '@' then

         State := STATE_EXPECTING_SUBDOMAIN

       else if c = '.' then

         State := STATE_LOCAL_PERIOD

       else

         break;

     STATE_LOCAL_PERIOD:

       if c in atom_chars then

         State := STATE_ATOM

       else if c = '"' then

         State := STATE_QTEXT

       else

         break;

     STATE_EXPECTING_SUBDOMAIN:

       if c in letters then

         State := STATE_SUBDOMAIN

       else

         break;

     STATE_SUBDOMAIN:

       if c = '.' then begin

         inc(subdomains);

         State := STATE_EXPECTING_SUBDOMAIN

       end else if c = '-' then

         State := STATE_HYPHEN

       else if not (c in letters_digits) then

         break;

     STATE_HYPHEN:

       if c in letters_digits then

         State := STATE_SUBDOMAIN

       else if c  '-' then

         break;

     end;

     inc(i);

   end;

   if i       Result := False

   else

     Result := (State = STATE_SUBDOMAIN) and (subdomains >= 2);

 end;