Check e-mail address

Posted in Internet

Use function IsValidEmail, if you want to check any e-mail address. This function checks syntax of each part of e-mail address.

function IsValidEmail(const Value: string): Boolean;

  function CheckAllowed(const s: string): Boolean;
  var i: Integer;
  begin
    Result:= false;
    for i:= 1 to Length(s) do
      if not (s[i] in ['a'..'z', 
                       'A'..'Z', 
                       '0'..'9', 
                       '_', 
                       '-', 
                       '.']) then Exit;
    Result:= true;
  end;

var
  i: Integer;
  NamePart, ServerPart: string;
begin
  Result:= False;
  i:=Pos('@', Value);
  if i=0 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;
  i:=Pos('.', ServerPart);
  if (i=0) or (i>(Length(serverPart)-2)) then Exit;
  Result:= CheckAllowed(NamePart) and CheckAllowed(ServerPart);
end;